Skip to content

Commit 06da2c8

Browse files
[flake8-async] fix detection for large integer sleep durations in ASYNC116 rule (astral-sh#18767)
Co-authored-by: Micha Reiser <[email protected]>
1 parent 55a2ff9 commit 06da2c8

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_async/ASYNC116.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,11 @@ async def test_trio_async116_helpers():
128128

129129
await trio.sleep(seconds=86401) # ASYNC116
130130
await trio.sleep(delay=86401) # OK
131+
132+
133+
async def _():
134+
import trio
135+
from trio import sleep
136+
137+
await sleep(18446744073709551616)
138+
await trio.sleep(99999999999999999999)

crates/ruff_linter/src/rules/flake8_async/rules/long_sleep_not_forever.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,10 @@ pub(crate) fn long_sleep_not_forever(checker: &Checker, call: &ExprCall) {
100100
// TODO(ekohilas): Replace with Duration::from_days(1).as_secs(); when available.
101101
let one_day_in_secs = 60 * 60 * 24;
102102
match value {
103-
Number::Int(int_value) => {
104-
let Some(int_value) = int_value.as_u64() else {
105-
return;
106-
};
107-
if int_value <= one_day_in_secs {
108-
return;
109-
}
110-
}
103+
Number::Int(int_value) => match int_value.as_u64() {
104+
Some(int_value) if int_value <= one_day_in_secs => return,
105+
_ => {} // The number is too large, and more than 24 hours
106+
},
111107
Number::Float(float_value) =>
112108
{
113109
#[expect(clippy::cast_precision_loss)]

crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC116_ASYNC116.py.snap

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,38 @@ ASYNC116.py:129:11: ASYNC116 [*] `trio.sleep()` with >24 hour interval should us
330330
129 |- await trio.sleep(seconds=86401) # ASYNC116
331331
129 |+ await trio.sleep_forever() # ASYNC116
332332
130 130 | await trio.sleep(delay=86401) # OK
333+
131 131 |
334+
132 132 |
335+
336+
ASYNC116.py:137:11: ASYNC116 [*] `trio.sleep()` with >24 hour interval should usually be `trio.sleep_forever()`
337+
|
338+
135 | from trio import sleep
339+
136 |
340+
137 | await sleep(18446744073709551616)
341+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ASYNC116
342+
138 | await trio.sleep(99999999999999999999)
343+
|
344+
= help: Replace with `trio.sleep_forever()`
345+
346+
Unsafe fix
347+
134 134 | import trio
348+
135 135 | from trio import sleep
349+
136 136 |
350+
137 |- await sleep(18446744073709551616)
351+
137 |+ await trio.sleep_forever()
352+
138 138 | await trio.sleep(99999999999999999999)
353+
354+
ASYNC116.py:138:11: ASYNC116 [*] `trio.sleep()` with >24 hour interval should usually be `trio.sleep_forever()`
355+
|
356+
137 | await sleep(18446744073709551616)
357+
138 | await trio.sleep(99999999999999999999)
358+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ASYNC116
359+
|
360+
= help: Replace with `trio.sleep_forever()`
361+
362+
Unsafe fix
363+
135 135 | from trio import sleep
364+
136 136 |
365+
137 137 | await sleep(18446744073709551616)
366+
138 |- await trio.sleep(99999999999999999999)
367+
138 |+ await trio.sleep_forever()

0 commit comments

Comments
 (0)