Skip to content

Commit

Permalink
Only flag flake8-trio rule when trio is present (#8550)
Browse files Browse the repository at this point in the history
## Summary

Hoping to avoid some false positives by narrowing the scope of
#8534.
  • Loading branch information
charliermarsh committed Nov 7, 2023
1 parent e2c7b1e commit 71e93a9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import trio


async def func():
...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use crate::checkers::ast::Checker;
/// trio's built-in timeout functionality, available as `trio.fail_after`,
/// `trio.move_on_after`, `trio.fail_at`, and `trio.move_on_at`.
///
/// ## Known problems
/// To avoid false positives, this rule is only enabled if `trio` is imported
/// in the module.
///
/// ## Example
/// ```python
/// async def func():
Expand Down Expand Up @@ -40,12 +44,19 @@ pub(crate) fn async_function_with_timeout(
checker: &mut Checker,
function_def: &ast::StmtFunctionDef,
) {
// Detect `async` calls with a `timeout` argument.
if !function_def.is_async {
return;
}
let Some(timeout) = function_def.parameters.find("timeout") else {
return;
};

// If `trio` isn't in scope, avoid raising the diagnostic.
if !checker.semantic().seen(&["trio"]) {
return;
}

checker.diagnostics.push(Diagnostic::new(
TrioAsyncFunctionWithTimeout,
timeout.range(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
source: crates/ruff_linter/src/rules/flake8_trio/mod.rs
---
TRIO109.py:5:16: TRIO109 Prefer `trio.fail_after` and `trio.move_on_after` over manual `async` timeout behavior
TRIO109.py:8:16: TRIO109 Prefer `trio.fail_after` and `trio.move_on_after` over manual `async` timeout behavior
|
5 | async def func(timeout):
8 | async def func(timeout):
| ^^^^^^^ TRIO109
6 | ...
9 | ...
|

TRIO109.py:9:16: TRIO109 Prefer `trio.fail_after` and `trio.move_on_after` over manual `async` timeout behavior
TRIO109.py:12:16: TRIO109 Prefer `trio.fail_after` and `trio.move_on_after` over manual `async` timeout behavior
|
9 | async def func(timeout=10):
12 | async def func(timeout=10):
| ^^^^^^^^^^ TRIO109
10 | ...
13 | ...
|


10 changes: 10 additions & 0 deletions crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,16 @@ impl<'a> SemanticModel<'a> {
exceptions
}

/// Return `true` if the module at the given path was seen anywhere in the semantic model.
/// This includes both direct imports (`import trio`) and member imports (`from trio import
/// TrioTask`).
pub fn seen(&self, module: &[&str]) -> bool {
self.bindings
.iter()
.filter_map(Binding::as_any_import)
.any(|import| import.call_path().starts_with(module))
}

/// Generate a [`Snapshot`] of the current semantic model.
pub fn snapshot(&self) -> Snapshot {
Snapshot {
Expand Down

0 comments on commit 71e93a9

Please sign in to comment.