Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only flag flake8-trio rule when trio is present #8550

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading