Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-async] Update ASYNC100 to match upstream #12221

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import anyio
import asyncio
import trio


Expand Down Expand Up @@ -25,3 +27,33 @@ async def func():
with trio.move_at():
async with trio.open_nursery() as nursery:
...


async def func():
with anyio.move_on_after():
...


async def func():
with anyio.fail_after():
...


async def func():
with anyio.CancelScope():
...


async def func():
with anyio.CancelScope():
...


async def func():
with asyncio.timeout():
...


async def func():
with asyncio.timeout_at():
...
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,8 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.enabled(Rule::UselessWithLock) {
pylint::rules::useless_with_lock(checker, with_stmt);
}
if checker.enabled(Rule::TrioTimeoutWithoutAwait) {
flake8_async::rules::timeout_without_await(checker, with_stmt, items);
if checker.enabled(Rule::CancelScopeNoCheckpoint) {
flake8_async::rules::cancel_scope_no_checkpoint(checker, with_stmt, items);
}
}
Stmt::While(while_stmt @ ast::StmtWhile { body, orelse, .. }) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Pylint, "W3301") => (RuleGroup::Stable, rules::pylint::rules::NestedMinMax),

// flake8-async
(Flake8Async, "100") => (RuleGroup::Stable, rules::flake8_async::rules::TrioTimeoutWithoutAwait),
(Flake8Async, "100") => (RuleGroup::Stable, rules::flake8_async::rules::CancelScopeNoCheckpoint),
(Flake8Async, "105") => (RuleGroup::Stable, rules::flake8_async::rules::TrioSyncCall),
(Flake8Async, "109") => (RuleGroup::Stable, rules::flake8_async::rules::TrioAsyncFunctionWithTimeout),
(Flake8Async, "110") => (RuleGroup::Stable, rules::flake8_async::rules::TrioUnneededSleep),
Expand Down
261 changes: 147 additions & 114 deletions crates/ruff_linter/src/rules/flake8_async/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,136 @@ use ruff_python_ast::name::QualifiedName;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(super) enum MethodName {
AcloseForcefully,
CancelScope,
CancelShieldedCheckpoint,
Checkpoint,
CheckpointIfCancelled,
FailAfter,
FailAt,
MoveOnAfter,
MoveOnAt,
OpenFile,
OpenProcess,
OpenSslOverTcpListeners,
OpenSslOverTcpStream,
OpenTcpListeners,
OpenTcpStream,
OpenUnixSocket,
PermanentlyDetachCoroutineObject,
ReattachDetachedCoroutineObject,
RunProcess,
ServeListeners,
ServeSslOverTcp,
ServeTcp,
Sleep,
SleepForever,
TemporarilyDetachCoroutineObject,
WaitReadable,
WaitTaskRescheduled,
WaitWritable,
AsyncIOTimeout,
AsyncIOTimeoutAt,
AnyIOMoveOnAfter,
AnyIOFailAfter,
AnyIOCancelScope,
TrioAcloseForcefully,
TrioCancelScope,
TrioCancelShieldedCheckpoint,
TrioCheckpoint,
TrioCheckpointIfCancelled,
TrioFailAfter,
TrioFailAt,
TrioMoveOnAfter,
TrioMoveOnAt,
TrioOpenFile,
TrioOpenProcess,
TrioOpenSslOverTcpListeners,
TrioOpenSslOverTcpStream,
TrioOpenTcpListeners,
TrioOpenTcpStream,
TrioOpenUnixSocket,
TrioPermanentlyDetachCoroutineObject,
TrioReattachDetachedCoroutineObject,
TrioRunProcess,
TrioServeListeners,
TrioServeSslOverTcp,
TrioServeTcp,
TrioSleep,
TrioSleepForever,
TrioTemporarilyDetachCoroutineObject,
TrioWaitReadable,
TrioWaitTaskRescheduled,
TrioWaitWritable,
}

impl MethodName {
/// Returns `true` if the method is async, `false` if it is sync.
pub(super) fn is_async(self) -> bool {
match self {
MethodName::AcloseForcefully
| MethodName::CancelShieldedCheckpoint
| MethodName::Checkpoint
| MethodName::CheckpointIfCancelled
| MethodName::OpenFile
| MethodName::OpenProcess
| MethodName::OpenSslOverTcpListeners
| MethodName::OpenSslOverTcpStream
| MethodName::OpenTcpListeners
| MethodName::OpenTcpStream
| MethodName::OpenUnixSocket
| MethodName::PermanentlyDetachCoroutineObject
| MethodName::ReattachDetachedCoroutineObject
| MethodName::RunProcess
| MethodName::ServeListeners
| MethodName::ServeSslOverTcp
| MethodName::ServeTcp
| MethodName::Sleep
| MethodName::SleepForever
| MethodName::TemporarilyDetachCoroutineObject
| MethodName::WaitReadable
| MethodName::WaitTaskRescheduled
| MethodName::WaitWritable => true,
matches!(
self,
MethodName::TrioAcloseForcefully
| MethodName::TrioCancelShieldedCheckpoint
| MethodName::TrioCheckpoint
| MethodName::TrioCheckpointIfCancelled
| MethodName::TrioOpenFile
| MethodName::TrioOpenProcess
| MethodName::TrioOpenSslOverTcpListeners
| MethodName::TrioOpenSslOverTcpStream
| MethodName::TrioOpenTcpListeners
| MethodName::TrioOpenTcpStream
| MethodName::TrioOpenUnixSocket
| MethodName::TrioPermanentlyDetachCoroutineObject
| MethodName::TrioReattachDetachedCoroutineObject
| MethodName::TrioRunProcess
| MethodName::TrioServeListeners
| MethodName::TrioServeSslOverTcp
| MethodName::TrioServeTcp
| MethodName::TrioSleep
| MethodName::TrioSleepForever
| MethodName::TrioTemporarilyDetachCoroutineObject
| MethodName::TrioWaitReadable
| MethodName::TrioWaitTaskRescheduled
| MethodName::TrioWaitWritable
)
}

MethodName::MoveOnAfter
| MethodName::MoveOnAt
| MethodName::FailAfter
| MethodName::FailAt
| MethodName::CancelScope => false,
}
/// Returns `true` if the method a timeout context manager.
pub(super) fn is_timeout_context(self) -> bool {
matches!(
self,
MethodName::AsyncIOTimeout
| MethodName::AsyncIOTimeoutAt
| MethodName::AnyIOMoveOnAfter
| MethodName::AnyIOFailAfter
| MethodName::AnyIOCancelScope
| MethodName::TrioMoveOnAfter
| MethodName::TrioMoveOnAt
| MethodName::TrioFailAfter
| MethodName::TrioFailAt
| MethodName::TrioCancelScope
)
}
}

impl MethodName {
pub(super) fn try_from(qualified_name: &QualifiedName<'_>) -> Option<Self> {
match qualified_name.segments() {
["trio", "CancelScope"] => Some(Self::CancelScope),
["trio", "aclose_forcefully"] => Some(Self::AcloseForcefully),
["trio", "fail_after"] => Some(Self::FailAfter),
["trio", "fail_at"] => Some(Self::FailAt),
["asyncio", "timeout"] => Some(Self::AsyncIOTimeout),
["asyncio", "timeout_at"] => Some(Self::AsyncIOTimeoutAt),
["anyio", "move_on_after"] => Some(Self::AnyIOMoveOnAfter),
["anyio", "fail_after"] => Some(Self::AnyIOFailAfter),
["anyio", "CancelScope"] => Some(Self::AnyIOCancelScope),
["trio", "CancelScope"] => Some(Self::TrioCancelScope),
["trio", "aclose_forcefully"] => Some(Self::TrioAcloseForcefully),
["trio", "fail_after"] => Some(Self::TrioFailAfter),
["trio", "fail_at"] => Some(Self::TrioFailAt),
["trio", "lowlevel", "cancel_shielded_checkpoint"] => {
Some(Self::CancelShieldedCheckpoint)
Some(Self::TrioCancelShieldedCheckpoint)
}
["trio", "lowlevel", "checkpoint"] => Some(Self::Checkpoint),
["trio", "lowlevel", "checkpoint_if_cancelled"] => Some(Self::CheckpointIfCancelled),
["trio", "lowlevel", "open_process"] => Some(Self::OpenProcess),
["trio", "lowlevel", "checkpoint"] => Some(Self::TrioCheckpoint),
["trio", "lowlevel", "checkpoint_if_cancelled"] => {
Some(Self::TrioCheckpointIfCancelled)
}
["trio", "lowlevel", "open_process"] => Some(Self::TrioOpenProcess),
["trio", "lowlevel", "permanently_detach_coroutine_object"] => {
Some(Self::PermanentlyDetachCoroutineObject)
Some(Self::TrioPermanentlyDetachCoroutineObject)
}
["trio", "lowlevel", "reattach_detached_coroutine_object"] => {
Some(Self::ReattachDetachedCoroutineObject)
Some(Self::TrioReattachDetachedCoroutineObject)
}
["trio", "lowlevel", "temporarily_detach_coroutine_object"] => {
Some(Self::TemporarilyDetachCoroutineObject)
Some(Self::TrioTemporarilyDetachCoroutineObject)
}
["trio", "lowlevel", "wait_readable"] => Some(Self::WaitReadable),
["trio", "lowlevel", "wait_task_rescheduled"] => Some(Self::WaitTaskRescheduled),
["trio", "lowlevel", "wait_writable"] => Some(Self::WaitWritable),
["trio", "move_on_after"] => Some(Self::MoveOnAfter),
["trio", "move_on_at"] => Some(Self::MoveOnAt),
["trio", "open_file"] => Some(Self::OpenFile),
["trio", "open_ssl_over_tcp_listeners"] => Some(Self::OpenSslOverTcpListeners),
["trio", "open_ssl_over_tcp_stream"] => Some(Self::OpenSslOverTcpStream),
["trio", "open_tcp_listeners"] => Some(Self::OpenTcpListeners),
["trio", "open_tcp_stream"] => Some(Self::OpenTcpStream),
["trio", "open_unix_socket"] => Some(Self::OpenUnixSocket),
["trio", "run_process"] => Some(Self::RunProcess),
["trio", "serve_listeners"] => Some(Self::ServeListeners),
["trio", "serve_ssl_over_tcp"] => Some(Self::ServeSslOverTcp),
["trio", "serve_tcp"] => Some(Self::ServeTcp),
["trio", "sleep"] => Some(Self::Sleep),
["trio", "sleep_forever"] => Some(Self::SleepForever),
["trio", "lowlevel", "wait_readable"] => Some(Self::TrioWaitReadable),
["trio", "lowlevel", "wait_task_rescheduled"] => Some(Self::TrioWaitTaskRescheduled),
["trio", "lowlevel", "wait_writable"] => Some(Self::TrioWaitWritable),
["trio", "move_on_after"] => Some(Self::TrioMoveOnAfter),
["trio", "move_on_at"] => Some(Self::TrioMoveOnAt),
["trio", "open_file"] => Some(Self::TrioOpenFile),
["trio", "open_ssl_over_tcp_listeners"] => Some(Self::TrioOpenSslOverTcpListeners),
["trio", "open_ssl_over_tcp_stream"] => Some(Self::TrioOpenSslOverTcpStream),
["trio", "open_tcp_listeners"] => Some(Self::TrioOpenTcpListeners),
["trio", "open_tcp_stream"] => Some(Self::TrioOpenTcpStream),
["trio", "open_unix_socket"] => Some(Self::TrioOpenUnixSocket),
["trio", "run_process"] => Some(Self::TrioRunProcess),
["trio", "serve_listeners"] => Some(Self::TrioServeListeners),
["trio", "serve_ssl_over_tcp"] => Some(Self::TrioServeSslOverTcp),
["trio", "serve_tcp"] => Some(Self::TrioServeTcp),
["trio", "sleep"] => Some(Self::TrioSleep),
["trio", "sleep_forever"] => Some(Self::TrioSleepForever),
_ => None,
}
}
Expand All @@ -116,42 +140,51 @@ impl MethodName {
impl std::fmt::Display for MethodName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MethodName::AcloseForcefully => write!(f, "trio.aclose_forcefully"),
MethodName::CancelScope => write!(f, "trio.CancelScope"),
MethodName::CancelShieldedCheckpoint => {
MethodName::AsyncIOTimeout => write!(f, "asyncio.timeout"),
MethodName::AsyncIOTimeoutAt => write!(f, "asyncio.timeout_at"),
MethodName::AnyIOMoveOnAfter => write!(f, "anyio.move_on_after"),
MethodName::AnyIOFailAfter => write!(f, "anyio.fail_after"),
MethodName::AnyIOCancelScope => write!(f, "anyio.CancelScope"),
MethodName::TrioAcloseForcefully => write!(f, "trio.aclose_forcefully"),
MethodName::TrioCancelScope => write!(f, "trio.CancelScope"),
MethodName::TrioCancelShieldedCheckpoint => {
write!(f, "trio.lowlevel.cancel_shielded_checkpoint")
}
MethodName::Checkpoint => write!(f, "trio.lowlevel.checkpoint"),
MethodName::CheckpointIfCancelled => write!(f, "trio.lowlevel.checkpoint_if_cancelled"),
MethodName::FailAfter => write!(f, "trio.fail_after"),
MethodName::FailAt => write!(f, "trio.fail_at"),
MethodName::MoveOnAfter => write!(f, "trio.move_on_after"),
MethodName::MoveOnAt => write!(f, "trio.move_on_at"),
MethodName::OpenFile => write!(f, "trio.open_file"),
MethodName::OpenProcess => write!(f, "trio.lowlevel.open_process"),
MethodName::OpenSslOverTcpListeners => write!(f, "trio.open_ssl_over_tcp_listeners"),
MethodName::OpenSslOverTcpStream => write!(f, "trio.open_ssl_over_tcp_stream"),
MethodName::OpenTcpListeners => write!(f, "trio.open_tcp_listeners"),
MethodName::OpenTcpStream => write!(f, "trio.open_tcp_stream"),
MethodName::OpenUnixSocket => write!(f, "trio.open_unix_socket"),
MethodName::PermanentlyDetachCoroutineObject => {
MethodName::TrioCheckpoint => write!(f, "trio.lowlevel.checkpoint"),
MethodName::TrioCheckpointIfCancelled => {
write!(f, "trio.lowlevel.checkpoint_if_cancelled")
}
MethodName::TrioFailAfter => write!(f, "trio.fail_after"),
MethodName::TrioFailAt => write!(f, "trio.fail_at"),
MethodName::TrioMoveOnAfter => write!(f, "trio.move_on_after"),
MethodName::TrioMoveOnAt => write!(f, "trio.move_on_at"),
MethodName::TrioOpenFile => write!(f, "trio.open_file"),
MethodName::TrioOpenProcess => write!(f, "trio.lowlevel.open_process"),
MethodName::TrioOpenSslOverTcpListeners => {
write!(f, "trio.open_ssl_over_tcp_listeners")
}
MethodName::TrioOpenSslOverTcpStream => write!(f, "trio.open_ssl_over_tcp_stream"),
MethodName::TrioOpenTcpListeners => write!(f, "trio.open_tcp_listeners"),
MethodName::TrioOpenTcpStream => write!(f, "trio.open_tcp_stream"),
MethodName::TrioOpenUnixSocket => write!(f, "trio.open_unix_socket"),
MethodName::TrioPermanentlyDetachCoroutineObject => {
write!(f, "trio.lowlevel.permanently_detach_coroutine_object")
}
MethodName::ReattachDetachedCoroutineObject => {
MethodName::TrioReattachDetachedCoroutineObject => {
write!(f, "trio.lowlevel.reattach_detached_coroutine_object")
}
MethodName::RunProcess => write!(f, "trio.run_process"),
MethodName::ServeListeners => write!(f, "trio.serve_listeners"),
MethodName::ServeSslOverTcp => write!(f, "trio.serve_ssl_over_tcp"),
MethodName::ServeTcp => write!(f, "trio.serve_tcp"),
MethodName::Sleep => write!(f, "trio.sleep"),
MethodName::SleepForever => write!(f, "trio.sleep_forever"),
MethodName::TemporarilyDetachCoroutineObject => {
MethodName::TrioRunProcess => write!(f, "trio.run_process"),
MethodName::TrioServeListeners => write!(f, "trio.serve_listeners"),
MethodName::TrioServeSslOverTcp => write!(f, "trio.serve_ssl_over_tcp"),
MethodName::TrioServeTcp => write!(f, "trio.serve_tcp"),
MethodName::TrioSleep => write!(f, "trio.sleep"),
MethodName::TrioSleepForever => write!(f, "trio.sleep_forever"),
MethodName::TrioTemporarilyDetachCoroutineObject => {
write!(f, "trio.lowlevel.temporarily_detach_coroutine_object")
}
MethodName::WaitReadable => write!(f, "trio.lowlevel.wait_readable"),
MethodName::WaitTaskRescheduled => write!(f, "trio.lowlevel.wait_task_rescheduled"),
MethodName::WaitWritable => write!(f, "trio.lowlevel.wait_writable"),
MethodName::TrioWaitReadable => write!(f, "trio.lowlevel.wait_readable"),
MethodName::TrioWaitTaskRescheduled => write!(f, "trio.lowlevel.wait_task_rescheduled"),
MethodName::TrioWaitWritable => write!(f, "trio.lowlevel.wait_writable"),
}
}
}
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/flake8_async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod tests {
use crate::settings::LinterSettings;
use crate::test::test_path;

#[test_case(Rule::TrioTimeoutWithoutAwait, Path::new("ASYNC100.py"))]
#[test_case(Rule::CancelScopeNoCheckpoint, Path::new("ASYNC100.py"))]
#[test_case(Rule::TrioSyncCall, Path::new("ASYNC105.py"))]
#[test_case(Rule::TrioAsyncFunctionWithTimeout, Path::new("ASYNC109.py"))]
#[test_case(Rule::TrioUnneededSleep, Path::new("ASYNC110.py"))]
Expand Down
Loading
Loading