From c50ec666602209d7d19ebe578c27bd2d0c20c4b8 Mon Sep 17 00:00:00 2001 From: augustelalande Date: Mon, 8 Jul 2024 15:26:13 -0400 Subject: [PATCH] Restrict to trio unless preview is enabled --- .../ruff_linter/src/rules/flake8_async/mod.rs | 22 ++++++++++++++++++- .../rules/async_function_with_timeout.rs | 18 +++++++++++---- ..._async__tests__ASYNC109_ASYNC109_1.py.snap | 12 ---------- ...ests__preview__ASYNC109_ASYNC109_0.py.snap | 16 ++++++++++++++ ...ests__preview__ASYNC109_ASYNC109_1.py.snap | 16 ++++++++++++++ 5 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__preview__ASYNC109_ASYNC109_0.py.snap create mode 100644 crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__preview__ASYNC109_ASYNC109_1.py.snap diff --git a/crates/ruff_linter/src/rules/flake8_async/mod.rs b/crates/ruff_linter/src/rules/flake8_async/mod.rs index 92d92c55ebac11..e2ccdd73768332 100644 --- a/crates/ruff_linter/src/rules/flake8_async/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_async/mod.rs @@ -9,10 +9,11 @@ mod tests { use anyhow::Result; use test_case::test_case; - use crate::assert_messages; use crate::registry::Rule; + use crate::settings::types::PreviewMode; use crate::settings::LinterSettings; use crate::test::test_path; + use crate::{assert_messages, settings}; #[test_case(Rule::TrioTimeoutWithoutAwait, Path::new("ASYNC100.py"))] #[test_case(Rule::TrioSyncCall, Path::new("ASYNC105.py"))] @@ -36,4 +37,23 @@ mod tests { assert_messages!(snapshot, diagnostics); Ok(()) } + + #[test_case(Rule::AsyncFunctionWithTimeout, Path::new("ASYNC109_0.py"))] + #[test_case(Rule::AsyncFunctionWithTimeout, Path::new("ASYNC109_1.py"))] + fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> { + let snapshot = format!( + "preview__{}_{}", + rule_code.noqa_code(), + path.to_string_lossy() + ); + let diagnostics = test_path( + Path::new("flake8_async").join(path).as_path(), + &settings::LinterSettings { + preview: PreviewMode::Enabled, + ..settings::LinterSettings::for_rule(rule_code) + }, + )?; + assert_messages!(snapshot, diagnostics); + Ok(()) + } } 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 7095d07be63ab8..d73d23ddc3d030 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::PreviewMode; /// ## What it does /// Checks for `async` functions with a `timeout` argument. @@ -81,8 +82,17 @@ pub(crate) fn async_function_with_timeout( AsyncModule::AsyncIO }; - checker.diagnostics.push(Diagnostic::new( - AsyncFunctionWithTimeout { module }, - timeout.range(), - )); + if matches!(checker.settings.preview, PreviewMode::Disabled) { + if matches!(module, AsyncModule::Trio) { + checker.diagnostics.push(Diagnostic::new( + AsyncFunctionWithTimeout { module }, + timeout.range(), + )); + } + } else { + checker.diagnostics.push(Diagnostic::new( + AsyncFunctionWithTimeout { module }, + timeout.range(), + )); + } } diff --git a/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_ASYNC109_1.py.snap b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_ASYNC109_1.py.snap index 18afabd7bf0c16..78704f66376733 100644 --- a/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_ASYNC109_1.py.snap +++ b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC109_ASYNC109_1.py.snap @@ -1,16 +1,4 @@ --- source: crates/ruff_linter/src/rules/flake8_async/mod.rs --- -ASYNC109_1.py:5:16: ASYNC109 Prefer using an async timeout context manager such as `asyncio.timeout` over reimplementing the functionality - | -5 | async def func(timeout): - | ^^^^^^^ ASYNC109 -6 | ... - | -ASYNC109_1.py:9:16: ASYNC109 Prefer using an async timeout context manager such as `asyncio.timeout` over reimplementing the functionality - | - 9 | async def func(timeout=10): - | ^^^^^^^^^^ ASYNC109 -10 | ... - | diff --git a/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__preview__ASYNC109_ASYNC109_0.py.snap b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__preview__ASYNC109_ASYNC109_0.py.snap new file mode 100644 index 00000000000000..649bf9a76f8f41 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__preview__ASYNC109_ASYNC109_0.py.snap @@ -0,0 +1,16 @@ +--- +source: crates/ruff_linter/src/rules/flake8_async/mod.rs +--- +ASYNC109_0.py:8:16: ASYNC109 Prefer using an async timeout context manager such as `trio.fail_after` over reimplementing the functionality + | +8 | async def func(timeout): + | ^^^^^^^ ASYNC109 +9 | ... + | + +ASYNC109_0.py:12:16: ASYNC109 Prefer using an async timeout context manager such as `trio.fail_after` over reimplementing the functionality + | +12 | async def func(timeout=10): + | ^^^^^^^^^^ ASYNC109 +13 | ... + | diff --git a/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__preview__ASYNC109_ASYNC109_1.py.snap b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__preview__ASYNC109_ASYNC109_1.py.snap new file mode 100644 index 00000000000000..18afabd7bf0c16 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__preview__ASYNC109_ASYNC109_1.py.snap @@ -0,0 +1,16 @@ +--- +source: crates/ruff_linter/src/rules/flake8_async/mod.rs +--- +ASYNC109_1.py:5:16: ASYNC109 Prefer using an async timeout context manager such as `asyncio.timeout` over reimplementing the functionality + | +5 | async def func(timeout): + | ^^^^^^^ ASYNC109 +6 | ... + | + +ASYNC109_1.py:9:16: ASYNC109 Prefer using an async timeout context manager such as `asyncio.timeout` over reimplementing the functionality + | + 9 | async def func(timeout=10): + | ^^^^^^^^^^ ASYNC109 +10 | ... + |