From bf8d84dcd291eaebcb84539b0408e00d0192da1f Mon Sep 17 00:00:00 2001 From: Jesse VanderWees Date: Wed, 21 Aug 2024 09:13:02 +0200 Subject: [PATCH 1/3] [async-function-with-timeout] Disable diagnostic for asyncio before Python3.11 (ASYNC109) --- .../rules/flake8_async/rules/async_function_with_timeout.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/ruff_linter/src/rules/flake8_async/rules/async_function_with_timeout.rs b/crates/ruff_linter/src/rules/flake8_async/rules/async_function_with_timeout.rs index 8edb208803315..4ceedafb5b435 100644 --- a/crates/ruff_linter/src/rules/flake8_async/rules/async_function_with_timeout.rs +++ b/crates/ruff_linter/src/rules/flake8_async/rules/async_function_with_timeout.rs @@ -6,6 +6,7 @@ use ruff_text_size::Ranged; use crate::checkers::ast::Checker; use crate::rules::flake8_async::helpers::AsyncModule; +use crate::settings::types::PythonVersion; /// ## What it does /// Checks for `async` functions with a `timeout` argument. @@ -86,6 +87,11 @@ pub(crate) fn async_function_with_timeout( AsyncModule::AsyncIo }; + // asyncio.timeout feature was first introduced in Python 3.11 + if module == AsyncModule::AsyncIo && checker.settings.target_version < PythonVersion::Py311 { + return; + } + checker.diagnostics.push(Diagnostic::new( AsyncFunctionWithTimeout { module }, timeout.range(), From 23adf3277f3a9bfd7a559929b350d7c3e4e846d2 Mon Sep 17 00:00:00 2001 From: Jesse VanderWees Date: Fri, 23 Aug 2024 09:03:57 +0200 Subject: [PATCH 2/3] add a test --- crates/ruff_linter/src/rules/flake8_async/mod.rs | 14 ++++++++++++++ ...async__tests__async109_python_310_or_older.snap | 4 ++++ 2 files changed, 18 insertions(+) create mode 100644 crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__async109_python_310_or_older.snap diff --git a/crates/ruff_linter/src/rules/flake8_async/mod.rs b/crates/ruff_linter/src/rules/flake8_async/mod.rs index 39da7b6e00973..9a75f84396beb 100644 --- a/crates/ruff_linter/src/rules/flake8_async/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_async/mod.rs @@ -11,6 +11,7 @@ mod tests { use crate::assert_messages; use crate::registry::Rule; + use crate::settings::types::PythonVersion; use crate::settings::LinterSettings; use crate::test::test_path; @@ -36,4 +37,17 @@ mod tests { assert_messages!(snapshot, diagnostics); Ok(()) } + + #[test] + fn async109_python_310_or_older() -> Result<()> { + let diagnostics = test_path( + Path::new("flake8_async").join("ASYNC109_1.py"), + &LinterSettings { + target_version: PythonVersion::Py310, + ..LinterSettings::for_rule(Rule::AsyncFunctionWithTimeout) + }, + )?; + assert_messages!(diagnostics); + Ok(()) + } } diff --git a/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__async109_python_310_or_older.snap b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__async109_python_310_or_older.snap new file mode 100644 index 0000000000000..78704f6637673 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__async109_python_310_or_older.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff_linter/src/rules/flake8_async/mod.rs +--- + From ad23c48129becf1a5e5bdc679d9b62772c35f962 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 23 Aug 2024 09:42:19 +0200 Subject: [PATCH 3/3] Add test for trio --- .../ruff_linter/src/rules/flake8_async/mod.rs | 9 +++++---- ...es__flake8_async__tests__ASYNC109_0.py.snap | 18 ++++++++++++++++++ ...s__flake8_async__tests__ASYNC109_1.py.snap} | 0 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_0.py.snap rename crates/ruff_linter/src/rules/flake8_async/snapshots/{ruff_linter__rules__flake8_async__tests__async109_python_310_or_older.snap => ruff_linter__rules__flake8_async__tests__ASYNC109_1.py.snap} (100%) diff --git a/crates/ruff_linter/src/rules/flake8_async/mod.rs b/crates/ruff_linter/src/rules/flake8_async/mod.rs index 9a75f84396beb..19b6551a88a6d 100644 --- a/crates/ruff_linter/src/rules/flake8_async/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_async/mod.rs @@ -38,16 +38,17 @@ mod tests { Ok(()) } - #[test] - fn async109_python_310_or_older() -> Result<()> { + #[test_case(Path::new("ASYNC109_0.py"); "asyncio")] + #[test_case(Path::new("ASYNC109_1.py"); "trio")] + fn async109_python_310_or_older(path: &Path) -> Result<()> { let diagnostics = test_path( - Path::new("flake8_async").join("ASYNC109_1.py"), + Path::new("flake8_async").join(path), &LinterSettings { target_version: PythonVersion::Py310, ..LinterSettings::for_rule(Rule::AsyncFunctionWithTimeout) }, )?; - assert_messages!(diagnostics); + assert_messages!(path.file_name().unwrap().to_str().unwrap(), diagnostics); Ok(()) } } diff --git a/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_0.py.snap b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_0.py.snap new file mode 100644 index 0000000000000..1a624f6dc47f6 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_0.py.snap @@ -0,0 +1,18 @@ +--- +source: crates/ruff_linter/src/rules/flake8_async/mod.rs +--- +ASYNC109_0.py:8:16: ASYNC109 Async function definition with a `timeout` parameter + | +8 | async def func(timeout): + | ^^^^^^^ ASYNC109 +9 | ... + | + = help: Use `trio.fail_after` instead + +ASYNC109_0.py:12:16: ASYNC109 Async function definition with a `timeout` parameter + | +12 | async def func(timeout=10): + | ^^^^^^^^^^ ASYNC109 +13 | ... + | + = help: Use `trio.fail_after` instead diff --git a/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__async109_python_310_or_older.snap b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_1.py.snap similarity index 100% rename from crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__async109_python_310_or_older.snap rename to crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_1.py.snap