Avoid raising TRIO115 violations for trio.sleep(...) calls with non-number values (#8532)

## Summary

Fixes bug in `TRIO115` where it would not `return` for values that were
not a `NumberLiteral` so
```python
x = "bla"
trio.sleep(x)
```
would set off a false positive

## Test Plan

Added test case to fixture
This commit is contained in:
qdegraaf
2023-11-06 23:49:12 +01:00
committed by GitHub
parent d71c65d0c8
commit 9a8400a287
3 changed files with 31 additions and 26 deletions

View File

@@ -17,6 +17,9 @@ async def func():
sleep(0) # TRIO115
bar = "bar"
trio.sleep(bar)
trio.sleep(0) # TRIO115

View File

@@ -77,14 +77,14 @@ pub(crate) fn zero_sleep_call(checker: &mut Checker, call: &ExprCall) {
})
| Stmt::AugAssign(ast::StmtAugAssign { value, .. }) = parent
{
if let Expr::NumberLiteral(ast::ExprNumberLiteral {
value: num, ..
}) = value.as_ref()
{
let Some(int) = num.as_int() else { return };
if *int != Int::ZERO {
return;
}
let Expr::NumberLiteral(ast::ExprNumberLiteral { value: num, .. }) =
value.as_ref()
else {
return;
};
let Some(int) = num.as_int() else { return };
if *int != Int::ZERO {
return;
}
}
}

View File

@@ -69,6 +69,8 @@ TRIO115.py:18:5: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.s
17 |
18 | sleep(0) # TRIO115
| ^^^^^^^^ TRIO115
19 |
20 | bar = "bar"
|
= help: Replace with `trio.lowlevel.checkpoint()`
@@ -79,39 +81,39 @@ TRIO115.py:18:5: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.s
18 |- sleep(0) # TRIO115
18 |+ trio.lowlevel.checkpoint # TRIO115
19 19 |
20 20 |
21 21 | trio.sleep(0) # TRIO115
20 20 | bar = "bar"
21 21 | trio.sleep(bar)
TRIO115.py:21:1: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)`
TRIO115.py:24:1: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)`
|
21 | trio.sleep(0) # TRIO115
24 | trio.sleep(0) # TRIO115
| ^^^^^^^^^^^^^ TRIO115
|
= help: Replace with `trio.lowlevel.checkpoint()`
Fix
18 18 | sleep(0) # TRIO115
19 19 |
20 20 |
21 |-trio.sleep(0) # TRIO115
21 |+trio.lowlevel.checkpoint # TRIO115
21 21 | trio.sleep(bar)
22 22 |
23 23 |
24 24 | def func():
24 |-trio.sleep(0) # TRIO115
24 |+trio.lowlevel.checkpoint # TRIO115
25 25 |
26 26 |
27 27 | def func():
TRIO115.py:25:14: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)`
TRIO115.py:28:14: TRIO115 [*] Use `trio.lowlevel.checkpoint()` instead of `trio.sleep(0)`
|
24 | def func():
25 | trio.run(trio.sleep(0)) # TRIO115
27 | def func():
28 | trio.run(trio.sleep(0)) # TRIO115
| ^^^^^^^^^^^^^ TRIO115
|
= help: Replace with `trio.lowlevel.checkpoint()`
Fix
22 22 |
23 23 |
24 24 | def func():
25 |- trio.run(trio.sleep(0)) # TRIO115
25 |+ trio.run(trio.lowlevel.checkpoint) # TRIO115
25 25 |
26 26 |
27 27 | def func():
28 |- trio.run(trio.sleep(0)) # TRIO115
28 |+ trio.run(trio.lowlevel.checkpoint) # TRIO115