From b20c165bb10eb8f61522d1d8551b34d043a66ffb Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 30 Nov 2023 22:03:58 -0500 Subject: [PATCH] Use full arguments range for zero-sleep-call --- .../test/fixtures/flake8_trio/TRIO115.py | 5 +++ .../flake8_trio/rules/zero_sleep_call.rs | 6 ++- ...lake8_trio__tests__TRIO115_TRIO115.py.snap | 40 ++++++++++++++++--- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_trio/TRIO115.py b/crates/ruff_linter/resources/test/fixtures/flake8_trio/TRIO115.py index 410408576acc6..aa25cb8e5a3ae 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_trio/TRIO115.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_trio/TRIO115.py @@ -26,5 +26,10 @@ def func(): from trio import Event, sleep + def func(): sleep(0) # TRIO115 + + +async def func(): + await sleep(seconds=0) # TRIO115 diff --git a/crates/ruff_linter/src/rules/flake8_trio/rules/zero_sleep_call.rs b/crates/ruff_linter/src/rules/flake8_trio/rules/zero_sleep_call.rs index 6ddd3d572d053..38470abbf99ae 100644 --- a/crates/ruff_linter/src/rules/flake8_trio/rules/zero_sleep_call.rs +++ b/crates/ruff_linter/src/rules/flake8_trio/rules/zero_sleep_call.rs @@ -16,12 +16,16 @@ use crate::importer::ImportRequest; /// /// ## Example /// ```python +/// import trio +/// /// async def func(): /// await trio.sleep(0) /// ``` /// /// Use instead: /// ```python +/// import trio +/// /// async def func(): /// await trio.lowlevel.checkpoint() /// ``` @@ -103,7 +107,7 @@ pub(crate) fn zero_sleep_call(checker: &mut Checker, call: &ExprCall) { )?; let reference_edit = Edit::range_replacement(format!("{binding}.checkpoint"), call.func.range()); - let arg_edit = Edit::range_deletion(arg.range()); + let arg_edit = Edit::range_replacement("()".to_string(), call.arguments.range()); Ok(Fix::safe_edits(import_edit, [reference_edit, arg_edit])) }); checker.diagnostics.push(diagnostic); diff --git a/crates/ruff_linter/src/rules/flake8_trio/snapshots/ruff_linter__rules__flake8_trio__tests__TRIO115_TRIO115.py.snap b/crates/ruff_linter/src/rules/flake8_trio/snapshots/ruff_linter__rules__flake8_trio__tests__TRIO115_TRIO115.py.snap index 6b4c34efd1197..0dfeef7c653fb 100644 --- a/crates/ruff_linter/src/rules/flake8_trio/snapshots/ruff_linter__rules__flake8_trio__tests__TRIO115_TRIO115.py.snap +++ b/crates/ruff_linter/src/rules/flake8_trio/snapshots/ruff_linter__rules__flake8_trio__tests__TRIO115_TRIO115.py.snap @@ -85,10 +85,10 @@ TRIO115.py:17:5: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.s 19 19 | bar = "bar" 20 20 | trio.sleep(bar) -TRIO115.py:30:5: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)` +TRIO115.py:31:5: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)` | -29 | def func(): -30 | sleep(0) # TRIO115 +30 | def func(): +31 | sleep(0) # TRIO115 | ^^^^^^^^ TRIO115 | = help: Replace with `trio.lowlevel.checkpoint()` @@ -100,8 +100,36 @@ TRIO115.py:30:5: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.s 27 |-from trio import Event, sleep 27 |+from trio import Event, sleep, lowlevel 28 28 | -29 29 | def func(): -30 |- sleep(0) # TRIO115 - 30 |+ lowlevel.checkpoint() # TRIO115 +29 29 | +30 30 | def func(): +31 |- sleep(0) # TRIO115 + 31 |+ lowlevel.checkpoint() # TRIO115 +32 32 | +33 33 | +34 34 | async def func(): + +TRIO115.py:35:11: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)` + | +34 | async def func(): +35 | await sleep(seconds=0) # TRIO115 + | ^^^^^^^^^^^^^^^^ TRIO115 + | + = help: Replace with `trio.lowlevel.checkpoint()` + +ℹ Safe fix +24 24 | trio.run(trio.sleep(0)) # TRIO115 +25 25 | +26 26 | +27 |-from trio import Event, sleep + 27 |+from trio import Event, sleep, lowlevel +28 28 | +29 29 | +30 30 | def func(): +-------------------------------------------------------------------------------- +32 32 | +33 33 | +34 34 | async def func(): +35 |- await sleep(seconds=0) # TRIO115 + 35 |+ await lowlevel.checkpoint() # TRIO115