mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 06:41:23 +00:00
[flake8-async
] Mark autofix for ASYNC115
as unsafe if the call expression contains comments (#18753)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
f7a741a99e
commit
136443b71b
3 changed files with 62 additions and 1 deletions
|
@ -165,3 +165,15 @@ async def test_trio_async115_helpers():
|
||||||
|
|
||||||
await trio.sleep(seconds=0) # ASYNC115
|
await trio.sleep(seconds=0) # ASYNC115
|
||||||
await trio.sleep(delay=0) # OK
|
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
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use ruff_diagnostics::Applicability;
|
||||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||||
use ruff_python_ast::{self as ast, Expr, ExprCall, Int, Number};
|
use ruff_python_ast::{self as ast, Expr, ExprCall, Int, Number};
|
||||||
use ruff_python_semantic::Modules;
|
use ruff_python_semantic::Modules;
|
||||||
|
@ -32,6 +33,21 @@ use crate::{AlwaysFixableViolation, Edit, Fix};
|
||||||
/// async def func():
|
/// async def func():
|
||||||
/// await trio.lowlevel.checkpoint()
|
/// 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)]
|
#[derive(ViolationMetadata)]
|
||||||
pub(crate) struct AsyncZeroSleep {
|
pub(crate) struct AsyncZeroSleep {
|
||||||
module: AsyncModule,
|
module: AsyncModule,
|
||||||
|
@ -119,7 +135,15 @@ pub(crate) fn async_zero_sleep(checker: &Checker, call: &ExprCall) {
|
||||||
let reference_edit =
|
let reference_edit =
|
||||||
Edit::range_replacement(format!("{binding}.checkpoint"), call.func.range());
|
Edit::range_replacement(format!("{binding}.checkpoint"), call.func.range());
|
||||||
let arg_edit = Edit::range_replacement("()".to_string(), call.arguments.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
|
||||||
|
},
|
||||||
|
))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.sleep(seconds=0) # ASYNC115
|
||||||
166 |+ await trio.lowlevel.checkpoint() # ASYNC115
|
166 |+ await trio.lowlevel.checkpoint() # ASYNC115
|
||||||
167 167 | await trio.sleep(delay=0) # OK
|
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…
Add table
Add a link
Reference in a new issue