-
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
[TRIO
] Add TRIO115
: TrioZeroSleepCall
#8486
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
26 changes: 26 additions & 0 deletions
26
crates/ruff_linter/resources/test/fixtures/flake8_trio/TRIO115.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,26 @@ | ||
import trio | ||
from trio import sleep | ||
|
||
|
||
async def afoo(): | ||
await trio.sleep(0) # TRIO115 | ||
await trio.sleep(1) # Ok | ||
await trio.sleep(0, 1) # Ok | ||
await trio.sleep(...) # Ok | ||
await trio.sleep() # Ok | ||
|
||
trio.sleep(0) # TRIO115 | ||
foo = 0 | ||
trio.sleep(foo) # TRIO115 | ||
trio.sleep(1) # OK | ||
time.sleep(0) # OK | ||
|
||
sleep(0) # TRIO115 | ||
|
||
|
||
trio.sleep(0) # TRIO115 | ||
|
||
|
||
def foo(): | ||
trio.run(trio.sleep(0)) # TRIO115 | ||
|
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,3 +1,5 @@ | ||
pub(crate) use timeout_without_await::*; | ||
pub(crate) use zero_sleep_call::*; | ||
|
||
mod timeout_without_await; | ||
mod zero_sleep_call; |
109 changes: 109 additions & 0 deletions
109
crates/ruff_linter/src/rules/flake8_trio/rules/zero_sleep_call.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,109 @@ | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::Stmt; | ||
use ruff_python_ast::{self as ast, Expr, ExprCall, Int}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::importer::ImportRequest; | ||
|
||
/// ## What it does | ||
/// Checks for calls of `trio.sleep(0)` and suggests replacement with `trio.lowlevel.checkpoint()` | ||
/// | ||
/// ## Why is this bad? | ||
/// Although equivalent to `trio.sleep(0)`, a call to `trio.lowlevel.checkpoint()` is more | ||
/// suggestive of the underlying process and the author's intent. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// async def func(): | ||
/// await trio.sleep(0) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// async def func(): | ||
/// await trio.lowlevel.checkpoint() | ||
/// ``` | ||
#[violation] | ||
pub struct TrioZeroSleepCall; | ||
|
||
impl AlwaysFixableViolation for TrioZeroSleepCall { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)`") | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
format!("Replace `trio.sleep(0)` with `trio.lowlevel.checkpoint()`") | ||
} | ||
} | ||
|
||
/// TRIO115 | ||
pub(crate) fn zero_sleep_call(checker: &mut Checker, call: &ExprCall) { | ||
if !checker | ||
.semantic() | ||
.resolve_call_path(call.func.as_ref()) | ||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["trio", "sleep"])) | ||
{ | ||
return; | ||
} | ||
|
||
if call.arguments.len() != 1 { | ||
return; | ||
} | ||
|
||
let Some(arg) = call.arguments.find_argument("seconds", 0) else { | ||
return; | ||
}; | ||
|
||
match arg { | ||
Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => { | ||
let Some(int) = value.as_int() else { return }; | ||
if *int != Int::ZERO { | ||
return; | ||
} | ||
} | ||
Expr::Name(ast::ExprName { id, .. }) => { | ||
let scope = checker.semantic().current_scope(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find a more compact way to do this yet. In case there is one somewhere in the repo already, happy to hear about it and refactor this. |
||
if let Some(binding_id) = scope.get(id) { | ||
let binding = checker.semantic().binding(binding_id); | ||
if binding.kind.is_assignment() || binding.kind.is_named_expr_assignment() { | ||
if let Some(parent_id) = binding.source { | ||
let parent = checker.semantic().statement(parent_id); | ||
if let Stmt::Assign(ast::StmtAssign { value, .. }) | ||
| Stmt::AnnAssign(ast::StmtAnnAssign { | ||
value: Some(value), .. | ||
}) | ||
| Stmt::AugAssign(ast::StmtAugAssign { value, .. }) = parent | ||
{ | ||
if let Expr::NumberLiteral(ast::ExprNumberLiteral { | ||
value: num, .. | ||
}) = value.as_ref() | ||
{ | ||
let Some(int) = num.as_int() else { return }; | ||
if *int != Int::ZERO { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
_ => return, | ||
} | ||
|
||
let mut diagnostic = Diagnostic::new(TrioZeroSleepCall, call.range()); | ||
diagnostic.try_set_fix(|| { | ||
let (import_edit, binding) = checker.importer().get_or_import_symbol( | ||
&ImportRequest::import("trio", "lowlevel.checkpoint"), | ||
call.func.start(), | ||
checker.semantic(), | ||
)?; | ||
let reference_edit = Edit::range_replacement(binding, call.func.range()); | ||
let arg_edit = Edit::range_deletion(call.arguments.range); | ||
Ok(Fix::unsafe_edits(import_edit, [reference_edit, arg_edit])) | ||
}); | ||
checker.diagnostics.push(diagnostic); | ||
} |
118 changes: 118 additions & 0 deletions
118
...les/flake8_trio/snapshots/ruff_linter__rules__flake8_trio__tests__TRIO115_TRIO115.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,118 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_trio/mod.rs | ||
--- | ||
TRIO115.py:6:11: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)` | ||
| | ||
5 | async def afoo(): | ||
6 | await trio.sleep(0) # TRIO115 | ||
| ^^^^^^^^^^^^^ TRIO115 | ||
7 | await trio.sleep(1) # Ok | ||
8 | await trio.sleep(0, 1) # Ok | ||
| | ||
= help: Replace `trio.sleep(0)` with `trio.lowlevel.checkpoint()` | ||
|
||
ℹ Suggested fix | ||
3 3 | | ||
4 4 | | ||
5 5 | async def afoo(): | ||
6 |- await trio.sleep(0) # TRIO115 | ||
6 |+ await trio.lowlevel.checkpoint # TRIO115 | ||
7 7 | await trio.sleep(1) # Ok | ||
8 8 | await trio.sleep(0, 1) # Ok | ||
9 9 | await trio.sleep(...) # Ok | ||
|
||
TRIO115.py:12:5: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)` | ||
| | ||
10 | await trio.sleep() # Ok | ||
11 | | ||
12 | trio.sleep(0) # TRIO115 | ||
| ^^^^^^^^^^^^^ TRIO115 | ||
13 | foo = 0 | ||
14 | trio.sleep(foo) # TRIO115 | ||
| | ||
= help: Replace `trio.sleep(0)` with `trio.lowlevel.checkpoint()` | ||
|
||
ℹ Suggested fix | ||
9 9 | await trio.sleep(...) # Ok | ||
10 10 | await trio.sleep() # Ok | ||
11 11 | | ||
12 |- trio.sleep(0) # TRIO115 | ||
12 |+ trio.lowlevel.checkpoint # TRIO115 | ||
13 13 | foo = 0 | ||
14 14 | trio.sleep(foo) # TRIO115 | ||
15 15 | trio.sleep(1) # OK | ||
|
||
TRIO115.py:14:5: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)` | ||
| | ||
12 | trio.sleep(0) # TRIO115 | ||
13 | foo = 0 | ||
14 | trio.sleep(foo) # TRIO115 | ||
| ^^^^^^^^^^^^^^^ TRIO115 | ||
15 | trio.sleep(1) # OK | ||
16 | time.sleep(0) # OK | ||
| | ||
= help: Replace `trio.sleep(0)` with `trio.lowlevel.checkpoint()` | ||
|
||
ℹ Suggested fix | ||
11 11 | | ||
12 12 | trio.sleep(0) # TRIO115 | ||
13 13 | foo = 0 | ||
14 |- trio.sleep(foo) # TRIO115 | ||
14 |+ trio.lowlevel.checkpoint # TRIO115 | ||
15 15 | trio.sleep(1) # OK | ||
16 16 | time.sleep(0) # OK | ||
17 17 | | ||
|
||
TRIO115.py:18:5: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)` | ||
| | ||
16 | time.sleep(0) # OK | ||
17 | | ||
18 | sleep(0) # TRIO115 | ||
| ^^^^^^^^ TRIO115 | ||
| | ||
= help: Replace `trio.sleep(0)` with `trio.lowlevel.checkpoint()` | ||
|
||
ℹ Suggested fix | ||
15 15 | trio.sleep(1) # OK | ||
16 16 | time.sleep(0) # OK | ||
17 17 | | ||
18 |- sleep(0) # TRIO115 | ||
18 |+ trio.lowlevel.checkpoint # TRIO115 | ||
19 19 | | ||
20 20 | | ||
21 21 | trio.sleep(0) # TRIO115 | ||
|
||
TRIO115.py:21:1: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)` | ||
| | ||
21 | trio.sleep(0) # TRIO115 | ||
| ^^^^^^^^^^^^^ TRIO115 | ||
| | ||
= help: Replace `trio.sleep(0)` with `trio.lowlevel.checkpoint()` | ||
|
||
ℹ Suggested fix | ||
18 18 | sleep(0) # TRIO115 | ||
19 19 | | ||
20 20 | | ||
21 |-trio.sleep(0) # TRIO115 | ||
21 |+trio.lowlevel.checkpoint # TRIO115 | ||
22 22 | | ||
23 23 | | ||
24 24 | def foo(): | ||
|
||
TRIO115.py:25:14: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)` | ||
| | ||
24 | def foo(): | ||
25 | trio.run(trio.sleep(0)) # TRIO115 | ||
| ^^^^^^^^^^^^^ TRIO115 | ||
| | ||
= help: Replace `trio.sleep(0)` with `trio.lowlevel.checkpoint()` | ||
|
||
ℹ Suggested fix | ||
22 22 | | ||
23 23 | | ||
24 24 | def foo(): | ||
25 |- trio.run(trio.sleep(0)) # TRIO115 | ||
25 |+ trio.run(trio.lowlevel.checkpoint) # TRIO115 | ||
26 26 | | ||
|
||
|
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.
Interpreted from upstream plugin, couldn't find additional info in docs or PRs on the reason. All input is welcome