-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add TRIO110 rule #8537
Merged
charliermarsh
merged 5 commits into
astral-sh:main
from
karpetrosyan:add-trio-110-suppor
Nov 7, 2023
Merged
Add TRIO110 rule #8537
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4d3e48b
Add TRIO110 rule
karpetrosyan 9683375
Fix doc
karpetrosyan ebfcfb0
Update crates/ruff_linter/src/rules/flake8_trio/rules/unneeded_sleep.rs
karpetrosyan f1e0d9e
Merge branch 'main' into add-trio-110-suppor
charliermarsh cdd9910
Tweaks
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
crates/ruff_linter/resources/test/fixtures/flake8_trio/TRIO110.py
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,16 @@ | ||
import trio | ||
|
||
|
||
async def func(): | ||
while True: | ||
await trio.sleep(10) | ||
|
||
|
||
async def func(): | ||
while True: | ||
await trio.sleep_until(10) | ||
|
||
|
||
async def func(): | ||
while True: | ||
trio.sleep(10) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
pub(crate) use sync_call::*; | ||
pub(crate) use timeout_without_await::*; | ||
pub(crate) use unneeded_sleep::*; | ||
pub(crate) use zero_sleep_call::*; | ||
|
||
mod sync_call; | ||
mod timeout_without_await; | ||
mod unneeded_sleep; | ||
mod zero_sleep_call; |
68 changes: 68 additions & 0 deletions
68
crates/ruff_linter/src/rules/flake8_trio/rules/unneeded_sleep.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,68 @@ | ||
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; | ||
|
||
/// ## What it does | ||
/// Checks for the use of `trio.sleep` 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 `wait()` on a `trio.Event`. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// DONE = False | ||
/// | ||
/// | ||
/// async def func(): | ||
/// while not DONE: | ||
/// await trio.sleep(1) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// DONE = trio.Event() | ||
/// | ||
/// | ||
/// async def func(): | ||
/// await DONE.wait() | ||
/// ``` | ||
#[violation] | ||
pub struct TrioUnneededSleep; | ||
|
||
impl Violation for TrioUnneededSleep { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use `trio.Event` instead of awaiting `trio.sleep` in a `while` loop") | ||
} | ||
} | ||
|
||
/// TRIO110 | ||
pub(crate) fn unneeded_sleep(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; | ||
}; | ||
|
||
if checker | ||
.semantic() | ||
.resolve_call_path(func.as_ref()) | ||
.is_some_and(|path| matches!(path.as_slice(), ["trio", "sleep" | "sleep_until"])) | ||
{ | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(TrioUnneededSleep, while_stmt.range())); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...les/flake8_trio/snapshots/ruff_linter__rules__flake8_trio__tests__TRIO110_TRIO110.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,22 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_trio/mod.rs | ||
--- | ||
TRIO110.py:5:5: TRIO110 Use `trio.Event` instead of awaiting `trio.sleep` in a `while` loop | ||
| | ||
4 | async def func(): | ||
5 | while True: | ||
| _____^ | ||
6 | | await trio.sleep(10) | ||
| |____________________________^ TRIO110 | ||
| | ||
|
||
TRIO110.py:10:5: TRIO110 Use `trio.Event` instead of awaiting `trio.sleep` in a `while` loop | ||
| | ||
9 | async def func(): | ||
10 | while True: | ||
| _____^ | ||
11 | | await trio.sleep_until(10) | ||
| |__________________________________^ TRIO110 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, we prefer passing in the typed node (
ast::StmtWhile
instead of the individual properties of it, likebody
). That way, users can't accidentally pass an AST node of the wrong type.