[flake8-async] fix detection for large integer sleep durations in ASYNC116 rule (#18767)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
chiri 2025-06-19 12:37:20 +03:00 committed by GitHub
parent 55a2ff91c7
commit 06da2c808f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 8 deletions

View file

@ -128,3 +128,11 @@ async def test_trio_async116_helpers():
await trio.sleep(seconds=86401) # ASYNC116
await trio.sleep(delay=86401) # OK
async def _():
import trio
from trio import sleep
await sleep(18446744073709551616)
await trio.sleep(99999999999999999999)

View file

@ -100,14 +100,10 @@ pub(crate) fn long_sleep_not_forever(checker: &Checker, call: &ExprCall) {
// TODO(ekohilas): Replace with Duration::from_days(1).as_secs(); when available.
let one_day_in_secs = 60 * 60 * 24;
match value {
Number::Int(int_value) => {
let Some(int_value) = int_value.as_u64() else {
return;
};
if int_value <= one_day_in_secs {
return;
}
}
Number::Int(int_value) => match int_value.as_u64() {
Some(int_value) if int_value <= one_day_in_secs => return,
_ => {} // The number is too large, and more than 24 hours
},
Number::Float(float_value) =>
{
#[expect(clippy::cast_precision_loss)]

View file

@ -330,3 +330,38 @@ ASYNC116.py:129:11: ASYNC116 [*] `trio.sleep()` with >24 hour interval should us
129 |- await trio.sleep(seconds=86401) # ASYNC116
129 |+ await trio.sleep_forever() # ASYNC116
130 130 | await trio.sleep(delay=86401) # OK
131 131 |
132 132 |
ASYNC116.py:137:11: ASYNC116 [*] `trio.sleep()` with >24 hour interval should usually be `trio.sleep_forever()`
|
135 | from trio import sleep
136 |
137 | await sleep(18446744073709551616)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ASYNC116
138 | await trio.sleep(99999999999999999999)
|
= help: Replace with `trio.sleep_forever()`
Unsafe fix
134 134 | import trio
135 135 | from trio import sleep
136 136 |
137 |- await sleep(18446744073709551616)
137 |+ await trio.sleep_forever()
138 138 | await trio.sleep(99999999999999999999)
ASYNC116.py:138:11: ASYNC116 [*] `trio.sleep()` with >24 hour interval should usually be `trio.sleep_forever()`
|
137 | await sleep(18446744073709551616)
138 | await trio.sleep(99999999999999999999)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ASYNC116
|
= help: Replace with `trio.sleep_forever()`
Unsafe fix
135 135 | from trio import sleep
136 136 |
137 137 | await sleep(18446744073709551616)
138 |- await trio.sleep(99999999999999999999)
138 |+ await trio.sleep_forever()