-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Adds TRIO109 from the [flake8-trio plugin](https://github.com/Zac-HD/flake8-trio). Relates to: #8451
- Loading branch information
1 parent
621e98f
commit e2c7b1e
Showing
9 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/flake8_trio/TRIO109.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
async def func(): | ||
... | ||
|
||
|
||
async def func(timeout): | ||
... | ||
|
||
|
||
async def func(timeout=10): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
crates/ruff_linter/src/rules/flake8_trio/rules/async_function_with_timeout.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast as ast; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `async` functions with a `timeout` argument. | ||
/// | ||
/// ## Why is this bad? | ||
/// Rather than implementing asynchronous timeout behavior manually, prefer | ||
/// trio's built-in timeout functionality, available as `trio.fail_after`, | ||
/// `trio.move_on_after`, `trio.fail_at`, and `trio.move_on_at`. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// async def func(): | ||
/// await long_running_task(timeout=2) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// async def func(): | ||
/// with trio.fail_after(2): | ||
/// await long_running_task() | ||
/// ``` | ||
#[violation] | ||
pub struct TrioAsyncFunctionWithTimeout; | ||
|
||
impl Violation for TrioAsyncFunctionWithTimeout { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Prefer `trio.fail_after` and `trio.move_on_after` over manual `async` timeout behavior") | ||
} | ||
} | ||
|
||
/// TRIO109 | ||
pub(crate) fn async_function_with_timeout( | ||
checker: &mut Checker, | ||
function_def: &ast::StmtFunctionDef, | ||
) { | ||
if !function_def.is_async { | ||
return; | ||
} | ||
let Some(timeout) = function_def.parameters.find("timeout") else { | ||
return; | ||
}; | ||
checker.diagnostics.push(Diagnostic::new( | ||
TrioAsyncFunctionWithTimeout, | ||
timeout.range(), | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...les/flake8_trio/snapshots/ruff_linter__rules__flake8_trio__tests__TRIO109_TRIO109.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 | ||
| | ||
5 | async def func(timeout): | ||
| ^^^^^^^ TRIO109 | ||
6 | ... | ||
| | ||
|
||
TRIO109.py:9:16: TRIO109 Prefer `trio.fail_after` and `trio.move_on_after` over manual `async` timeout behavior | ||
| | ||
9 | async def func(timeout=10): | ||
| ^^^^^^^^^^ TRIO109 | ||
10 | ... | ||
| | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.