diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_async/ASYNC250.py b/crates/ruff_linter/resources/test/fixtures/flake8_async/ASYNC250.py index c5b3e1fd61827..4701fa8b4978a 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_async/ASYNC250.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_async/ASYNC250.py @@ -20,3 +20,10 @@ def foo(): async def foo(): builtins.input("testing") # ASYNC250 fake.input("whatever") # Ok + +# Regression test for https://github.com/astral-sh/ruff/issues/23425 +import asyncio + +async def main() -> None: + input("sync") # should emit here + await asyncio.to_thread(lambda: input("async")) # but not here diff --git a/crates/ruff_linter/src/checkers/ast/mod.rs b/crates/ruff_linter/src/checkers/ast/mod.rs index a083846cdf00c..47eae115822c4 100644 --- a/crates/ruff_linter/src/checkers/ast/mod.rs +++ b/crates/ruff_linter/src/checkers/ast/mod.rs @@ -797,17 +797,7 @@ impl SemanticSyntaxContext for Checker<'_> { } fn in_async_context(&self) -> bool { - for scope in self.semantic.current_scopes() { - match scope.kind { - ScopeKind::Class(_) | ScopeKind::Lambda(_) => return false, - ScopeKind::Function(ast::StmtFunctionDef { is_async, .. }) => return *is_async, - ScopeKind::Generator { .. } - | ScopeKind::Module - | ScopeKind::Type - | ScopeKind::DunderClassCell => {} - } - } - false + self.semantic.in_async_context() } fn in_await_allowed_context(&self) -> bool { diff --git a/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC250_ASYNC250.py.snap b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC250_ASYNC250.py.snap index 046faa42de00b..22deadaa6ce33 100644 --- a/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC250_ASYNC250.py.snap +++ b/crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC250_ASYNC250.py.snap @@ -27,3 +27,12 @@ ASYNC250 Blocking call to `input()` in async context | ^^^^^^^^^^^^^^ 22 | fake.input("whatever") # Ok | + +ASYNC250 Blocking call to `input()` in async context + --> ASYNC250.py:28:5 + | +27 | async def main() -> None: +28 | input("sync") # should emit here + | ^^^^^ +29 | await asyncio.to_thread(lambda: input("async")) # but not here + | diff --git a/crates/ruff_python_semantic/src/model.rs b/crates/ruff_python_semantic/src/model.rs index 78536071d718c..17d2a26fa49a5 100644 --- a/crates/ruff_python_semantic/src/model.rs +++ b/crates/ruff_python_semantic/src/model.rs @@ -1595,8 +1595,13 @@ impl<'a> SemanticModel<'a> { /// Return `true` if the model is in an async context. pub fn in_async_context(&self) -> bool { for scope in self.current_scopes() { - if let ScopeKind::Function(ast::StmtFunctionDef { is_async, .. }) = scope.kind { - return *is_async; + match scope.kind { + ScopeKind::Class(_) | ScopeKind::Lambda(_) => return false, + ScopeKind::Function(ast::StmtFunctionDef { is_async, .. }) => return *is_async, + ScopeKind::Generator { .. } + | ScopeKind::Module + | ScopeKind::Type + | ScopeKind::DunderClassCell => {} } } false