-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Update the name of `ASYNC110` to match [upstream](https://flake8-async.readthedocs.io/en/latest/rules.html). Also update to the functionality to match upstream by adding support for `asyncio` and `anyio` (gated behind preview). Part of #12039. ## Test Plan Added tests for `asyncio` and `anyio`
- Loading branch information
1 parent
88abc6a
commit 855d62c
Showing
10 changed files
with
210 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
crates/ruff_linter/src/rules/flake8_async/rules/async_busy_wait.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self as ast, Expr, Stmt}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::rules::flake8_async::helpers::AsyncModule; | ||
use crate::settings::types::PreviewMode; | ||
|
||
/// ## What it does | ||
/// Checks for the use of an async sleep function in a `while` loop. | ||
/// | ||
/// ## Why is this bad? | ||
/// Instead of sleeping in a `while` loop, and waiting for a condition | ||
/// to become true, it's preferable to `await` on an `Event` object such | ||
/// as: `asyncio.Event`, `trio.Event`, or `anyio.Event`. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// DONE = False | ||
/// | ||
/// | ||
/// async def func(): | ||
/// while not DONE: | ||
/// await asyncio.sleep(1) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// DONE = asyncio.Event() | ||
/// | ||
/// | ||
/// async def func(): | ||
/// await DONE.wait() | ||
/// ``` | ||
/// | ||
/// [`asyncio` events]: https://docs.python.org/3/library/asyncio-sync.html#asyncio.Event | ||
/// [`anyio` events]: https://trio.readthedocs.io/en/latest/reference-core.html#trio.Event | ||
/// [`trio` events]: https://anyio.readthedocs.io/en/latest/api.html#anyio.Event | ||
#[violation] | ||
pub struct AsyncBusyWait { | ||
module: AsyncModule, | ||
} | ||
|
||
impl Violation for AsyncBusyWait { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Self { module } = self; | ||
format!("Use `{module}.Event` instead of awaiting `{module}.sleep` in a `while` loop") | ||
} | ||
} | ||
|
||
/// ASYNC110 | ||
pub(crate) fn async_busy_wait(checker: &mut Checker, while_stmt: &ast::StmtWhile) { | ||
// The body should be a single `await` call. | ||
let [stmt] = while_stmt.body.as_slice() else { | ||
return; | ||
}; | ||
let Stmt::Expr(ast::StmtExpr { value, .. }) = stmt else { | ||
return; | ||
}; | ||
let Expr::Await(ast::ExprAwait { value, .. }) = value.as_ref() else { | ||
return; | ||
}; | ||
let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() else { | ||
return; | ||
}; | ||
|
||
let Some(qualified_name) = checker.semantic().resolve_qualified_name(func.as_ref()) else { | ||
return; | ||
}; | ||
|
||
if matches!(checker.settings.preview, PreviewMode::Disabled) { | ||
if matches!(qualified_name.segments(), ["trio", "sleep" | "sleep_until"]) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
AsyncBusyWait { | ||
module: AsyncModule::Trio, | ||
}, | ||
while_stmt.range(), | ||
)); | ||
} | ||
} else { | ||
if matches!( | ||
qualified_name.segments(), | ||
["trio" | "anyio", "sleep" | "sleep_until"] | ["asyncio", "sleep"] | ||
) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
AsyncBusyWait { | ||
module: AsyncModule::try_from(&qualified_name).unwrap(), | ||
}, | ||
while_stmt.range(), | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 0 additions & 73 deletions
73
crates/ruff_linter/src/rules/flake8_async/rules/unneeded_sleep.rs
This file was deleted.
Oops, something went wrong.
16 changes: 8 additions & 8 deletions
16
...flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC110_ASYNC110.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_async/mod.rs | ||
--- | ||
ASYNC110.py:5:5: ASYNC110 Use `trio.Event` instead of awaiting `trio.sleep` in a `while` loop | ||
ASYNC110.py:7:5: ASYNC110 Use `trio.Event` instead of awaiting `trio.sleep` in a `while` loop | ||
| | ||
4 | async def func(): | ||
5 | while True: | ||
6 | async def func(): | ||
7 | while True: | ||
| _____^ | ||
6 | | await trio.sleep(10) | ||
8 | | await trio.sleep(10) | ||
| |____________________________^ ASYNC110 | ||
| | ||
|
||
ASYNC110.py:10:5: ASYNC110 Use `trio.Event` instead of awaiting `trio.sleep` in a `while` loop | ||
ASYNC110.py:12:5: ASYNC110 Use `trio.Event` instead of awaiting `trio.sleep` in a `while` loop | ||
| | ||
9 | async def func(): | ||
10 | while True: | ||
11 | async def func(): | ||
12 | while True: | ||
| _____^ | ||
11 | | await trio.sleep_until(10) | ||
13 | | await trio.sleep_until(10) | ||
| |__________________________________^ ASYNC110 | ||
| |
47 changes: 47 additions & 0 deletions
47
...ync/snapshots/ruff_linter__rules__flake8_async__tests__preview__ASYNC110_ASYNC110.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_async/mod.rs | ||
--- | ||
ASYNC110.py:7:5: ASYNC110 Use `trio.Event` instead of awaiting `trio.sleep` in a `while` loop | ||
| | ||
6 | async def func(): | ||
7 | while True: | ||
| _____^ | ||
8 | | await trio.sleep(10) | ||
| |____________________________^ ASYNC110 | ||
| | ||
|
||
ASYNC110.py:12:5: ASYNC110 Use `trio.Event` instead of awaiting `trio.sleep` in a `while` loop | ||
| | ||
11 | async def func(): | ||
12 | while True: | ||
| _____^ | ||
13 | | await trio.sleep_until(10) | ||
| |__________________________________^ ASYNC110 | ||
| | ||
|
||
ASYNC110.py:22:5: ASYNC110 Use `asyncio.Event` instead of awaiting `asyncio.sleep` in a `while` loop | ||
| | ||
21 | async def func(): | ||
22 | while True: | ||
| _____^ | ||
23 | | await anyio.sleep(10) | ||
| |_____________________________^ ASYNC110 | ||
| | ||
|
||
ASYNC110.py:27:5: ASYNC110 Use `asyncio.Event` instead of awaiting `asyncio.sleep` in a `while` loop | ||
| | ||
26 | async def func(): | ||
27 | while True: | ||
| _____^ | ||
28 | | await anyio.sleep_until(10) | ||
| |___________________________________^ ASYNC110 | ||
| | ||
|
||
ASYNC110.py:37:5: ASYNC110 Use `anyio.Event` instead of awaiting `anyio.sleep` in a `while` loop | ||
| | ||
36 | async def func(): | ||
37 | while True: | ||
| _____^ | ||
38 | | await asyncio.sleep(10) | ||
| |_______________________________^ ASYNC110 | ||
| |