Skip to content

Commit

Permalink
Restrict to trio unless preview is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
augustelalande committed Jul 8, 2024
1 parent 4ef2faf commit c50ec66
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
22 changes: 21 additions & 1 deletion crates/ruff_linter/src/rules/flake8_async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand All @@ -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(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(),
));
}
}
Original file line number Diff line number Diff line change
@@ -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 | ...
|
Original file line number Diff line number Diff line change
@@ -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 | ...
|
Original file line number Diff line number Diff line change
@@ -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 | ...
|

0 comments on commit c50ec66

Please sign in to comment.