Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -177,3 +177,12 @@ async def func():
0 # comment
)
)


async def func():
# https://github.com/astral-sh/ruff/issues/21693
# The autofix for anyio should use `import anyio.lowlevel` instead of
# `from anyio import lowlevel`, since `anyio.lowlevel` is a submodule.
from anyio import sleep as anyio_sleep

await anyio_sleep(0) # ASYNC115
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{AlwaysFixableViolation, Edit, Fix};
///
/// Use instead:
/// ```python
/// import trio
/// import trio.lowlevel
///
///
/// async def func():
Expand Down Expand Up @@ -128,13 +128,18 @@ pub(crate) fn async_zero_sleep(checker: &Checker, call: &ExprCall) {

let mut diagnostic = checker.report_diagnostic(AsyncZeroSleep { module }, call.range());
diagnostic.try_set_fix(|| {
// `anyio.lowlevel` is a submodule, so we need `import anyio.lowlevel`
// rather than `from anyio import lowlevel`.
let full_module_name = format!("{module}.lowlevel");

let (import_edit, binding) = checker.importer().get_or_import_symbol(
&ImportRequest::import_from(&module.to_string(), "lowlevel"),
&ImportRequest::import(&full_module_name, "checkpoint"),
call.func.start(),
checker.semantic(),
)?;
let reference_edit =
Edit::range_replacement(format!("{binding}.checkpoint"), call.func.range());

let reference_edit = Edit::range_replacement(binding, call.func.range());

let arg_edit = Edit::range_replacement("()".to_string(), call.arguments.range());
Ok(Fix::applicable_edits(
import_edit,
Expand Down
Loading
Loading