Skip to content

Commit

Permalink
Synchronize message too
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 9, 2024
1 parent 696f81e commit 27f7715
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
7 changes: 5 additions & 2 deletions crates/ruff_linter/src/rules/flake8_async/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use ruff_python_ast::name::QualifiedName;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(super) enum AsyncModule {
AnyIO,
AsyncIO,
/// `anyio`
AnyIo,
/// `asyncio`
AsyncIo,
/// `trio`
Trio,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use crate::settings::types::PreviewMode;
/// await long_running_task()
/// ```
///
/// [asyncio timeouts]: https://docs.python.org/3/library/asyncio-task.html#timeouts
/// [anyio timeouts]: https://anyio.readthedocs.io/en/stable/cancellation.html
/// [trio timeouts]: https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-and-timeouts
/// [`asyncio` timeouts]: https://docs.python.org/3/library/asyncio-task.html#timeouts
/// [`anyio` timeouts]: https://anyio.readthedocs.io/en/stable/cancellation.html
/// [`trio` timeouts]: https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-and-timeouts
#[violation]
pub struct AsyncFunctionWithTimeout {
module: AsyncModule,
Expand All @@ -48,13 +48,17 @@ pub struct AsyncFunctionWithTimeout {
impl Violation for AsyncFunctionWithTimeout {
#[derive_message_formats]
fn message(&self) -> String {
format!("Async function definition with a `timeout` parameter")
}

fn fix_title(&self) -> Option<String> {
let Self { module } = self;
let recommendation = match module {
AsyncModule::AnyIO => "anyio.fail_after",
AsyncModule::AnyIo => "anyio.fail_after",
AsyncModule::Trio => "trio.fail_after",
AsyncModule::AsyncIO => "asyncio.timeout",
AsyncModule::AsyncIo => "asyncio.timeout",
};
format!("Prefer using an async timeout context manager such as `{recommendation}` over reimplementing the functionality")
Some(format!("Use `{recommendation}` instead"))
}
}

Expand All @@ -75,11 +79,11 @@ pub(crate) fn async_function_with_timeout(

// Get preferred module.
let module = if checker.semantic().seen_module(Modules::ANYIO) {
AsyncModule::AnyIO
AsyncModule::AnyIo
} else if checker.semantic().seen_module(Modules::TRIO) {
AsyncModule::Trio
} else {
AsyncModule::AsyncIO
AsyncModule::AsyncIo
};

if matches!(checker.settings.preview, PreviewMode::Disabled) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
---
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
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 Prefer using an async timeout context manager such as `trio.fail_after` over reimplementing the functionality
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
---
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
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 Prefer using an async timeout context manager such as `trio.fail_after` over reimplementing the functionality
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
---
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
ASYNC109_1.py:5:16: ASYNC109 Async function definition with a `timeout` parameter
|
5 | async def func(timeout):
| ^^^^^^^ ASYNC109
6 | ...
|
= help: Use `asyncio.timeout` instead

ASYNC109_1.py:9:16: ASYNC109 Prefer using an async timeout context manager such as `asyncio.timeout` over reimplementing the functionality
ASYNC109_1.py:9:16: ASYNC109 Async function definition with a `timeout` parameter
|
9 | async def func(timeout=10):
| ^^^^^^^^^^ ASYNC109
10 | ...
|
= help: Use `asyncio.timeout` instead

0 comments on commit 27f7715

Please sign in to comment.