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

[TRIO] Add TRIO109 rule #8534

Merged
merged 5 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,9 @@

async def foo():
...

async def foo(timeout):
...

async def foo(timeout=10):
...
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
flake8_builtins::rules::builtin_variable_shadowing(checker, name, name.range());
}
}
if checker.enabled(Rule::TrioAsyncFunctionWithTimeout) {
flake8_trio::rules::async_function_with_timeout(checker, parameters, *is_async);
}
#[cfg(feature = "unreachable-code")]
if checker.enabled(Rule::UnreachableCode) {
checker
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
// flake8-trio
(Flake8Trio, "100") => (RuleGroup::Preview, rules::flake8_trio::rules::TrioTimeoutWithoutAwait),
(Flake8Trio, "105") => (RuleGroup::Preview, rules::flake8_trio::rules::TrioSyncCall),
(Flake8Trio, "109") => (RuleGroup::Preview, rules::flake8_trio::rules::TrioAsyncFunctionWithTimeout),
(Flake8Trio, "115") => (RuleGroup::Preview, rules::flake8_trio::rules::TrioZeroSleepCall),

// flake8-builtins
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/flake8_trio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod tests {

#[test_case(Rule::TrioTimeoutWithoutAwait, Path::new("TRIO100.py"))]
#[test_case(Rule::TrioSyncCall, Path::new("TRIO105.py"))]
#[test_case(Rule::TrioAsyncFunctionWithTimeout, Path::new("TRIO109.py"))]
#[test_case(Rule::TrioZeroSleepCall, Path::new("TRIO115.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::checkers::ast::Checker;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::Parameters;

/// ## What it does
/// Checks for asynchronous functions that have a timeout argument.
///
/// ## Why is this bad?
/// It is preferable to use the special functions that Trio
/// provides to handle timeouts rather than implement them manually.
///
/// ## 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!("Async function definition with a `timeout` parameter - use `trio.[fail/move_on]_[after/at]` instead")
}
}

pub(crate) fn async_function_with_timeout(
checker: &mut Checker,
parameters: &Parameters,
is_async: bool,
) {
if !is_async {
return;
}

if let Some(timeout_argument) = parameters
.args
.iter()
.find(|argument| argument.parameter.name.eq("timeout"))
{
checker.diagnostics.push(Diagnostic::new(
TrioAsyncFunctionWithTimeout,
timeout_argument.range,
));
}
}
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/flake8_trio/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub(crate) use async_function_with_timeout::*;
pub(crate) use sync_call::*;
pub(crate) use timeout_without_await::*;
pub(crate) use zero_sleep_call::*;

mod async_function_with_timeout;
mod sync_call;
mod timeout_without_await;
mod zero_sleep_call;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/ruff_linter/src/rules/flake8_trio/mod.rs
---
TRIO109.py:5:15: TRIO109 Async function definition with a `timeout` parameter - use `trio.[fail/move_on]_[after/at]` instead
|
3 | ...
4 |
5 | async def foo(timeout):
| ^^^^^^^ TRIO109
6 | ...
|

TRIO109.py:8:15: TRIO109 Async function definition with a `timeout` parameter - use `trio.[fail/move_on]_[after/at]` instead
|
6 | ...
7 |
8 | async def foo(timeout=10):
| ^^^^^^^^^^ TRIO109
9 | ...
|


1 change: 1 addition & 0 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.