Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -165,3 +165,15 @@ async def test_trio_async115_helpers():

await trio.sleep(seconds=0) # ASYNC115
await trio.sleep(delay=0) # OK

# https://github.com/astral-sh/ruff/issues/18740
# The autofix for this is unsafe due to the comments.
async def func():
import trio

await (
trio # comment
.sleep( # comment
0 # comment
)
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ruff_diagnostics::Applicability;
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::{self as ast, Expr, ExprCall, Int, Number};
use ruff_python_semantic::Modules;
Expand Down Expand Up @@ -32,6 +33,21 @@ use crate::{AlwaysFixableViolation, Edit, Fix};
/// async def func():
/// await trio.lowlevel.checkpoint()
/// ```
/// ## Fix safety
/// This rule's fix is marked as unsafe if there's comments in the
/// `trio.sleep(0)` expression, as comments may be removed.
///
/// For example, the fix would be marked as unsafe in the following case:
/// ```python
/// import trio
///
///
/// async def func():
/// await trio.sleep( # comment
/// # comment
/// 0
/// )
/// ```
#[derive(ViolationMetadata)]
pub(crate) struct AsyncZeroSleep {
module: AsyncModule,
Expand Down Expand Up @@ -119,7 +135,15 @@ pub(crate) fn async_zero_sleep(checker: &Checker, call: &ExprCall) {
let reference_edit =
Edit::range_replacement(format!("{binding}.checkpoint"), call.func.range());
let arg_edit = Edit::range_replacement("()".to_string(), call.arguments.range());
Ok(Fix::safe_edits(import_edit, [reference_edit, arg_edit]))
Ok(Fix::applicable_edits(
import_edit,
[reference_edit, arg_edit],
if checker.comment_ranges().intersects(call.range()) {
Applicability::Unsafe
} else {
Applicability::Safe
},
))
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,28 @@ ASYNC115.py:166:11: ASYNC115 [*] Use `trio.lowlevel.checkpoint()` instead of `tr
166 |- await trio.sleep(seconds=0) # ASYNC115
166 |+ await trio.lowlevel.checkpoint() # ASYNC115
167 167 | await trio.sleep(delay=0) # OK
168 168 |
169 169 | # https://github.com/astral-sh/ruff/issues/18740

ASYNC115.py:175:5: ASYNC115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)`
|
174 | await (
175 | / trio # comment
176 | | .sleep( # comment
177 | | 0 # comment
178 | | )
| |_____^ ASYNC115
179 | )
|
= help: Replace with `trio.lowlevel.checkpoint()`

ℹ Unsafe fix
172 172 | import trio
173 173 |
174 174 | await (
175 |- trio # comment
176 |- .sleep( # comment
177 |- 0 # comment
178 |- )
175 |+ trio.lowlevel.checkpoint()
179 176 | )
Loading