Skip to content

Commit 1465b5d

Browse files
authored
[flake8-async] Fix in_async_context logic (#23426)
We move the logic from `Checker::in_async_context` (from when it implements `SemanticSyntaxContext`) into the similarly named method in `SemanticModel`. This affects the following rules: - ASYNC105 - ASYNC210 - ASYNC212 - ASYNC220 - ASYNC221 - ASYNC222 - ASYNC230 - ASYNC240 - ASYNC250 - ASYNC251 - UP028 Closes #23425
1 parent 410902f commit 1465b5d

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

crates/ruff_linter/resources/test/fixtures/flake8_async/ASYNC250.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ def foo():
2020
async def foo():
2121
builtins.input("testing") # ASYNC250
2222
fake.input("whatever") # Ok
23+
24+
# Regression test for https://github.com/astral-sh/ruff/issues/23425
25+
import asyncio
26+
27+
async def main() -> None:
28+
input("sync") # should emit here
29+
await asyncio.to_thread(lambda: input("async")) # but not here

crates/ruff_linter/src/checkers/ast/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -797,17 +797,7 @@ impl SemanticSyntaxContext for Checker<'_> {
797797
}
798798

799799
fn in_async_context(&self) -> bool {
800-
for scope in self.semantic.current_scopes() {
801-
match scope.kind {
802-
ScopeKind::Class(_) | ScopeKind::Lambda(_) => return false,
803-
ScopeKind::Function(ast::StmtFunctionDef { is_async, .. }) => return *is_async,
804-
ScopeKind::Generator { .. }
805-
| ScopeKind::Module
806-
| ScopeKind::Type
807-
| ScopeKind::DunderClassCell => {}
808-
}
809-
}
810-
false
800+
self.semantic.in_async_context()
811801
}
812802

813803
fn in_await_allowed_context(&self) -> bool {

crates/ruff_linter/src/rules/flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC250_ASYNC250.py.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ ASYNC250 Blocking call to `input()` in async context
2727
| ^^^^^^^^^^^^^^
2828
22 | fake.input("whatever") # Ok
2929
|
30+
31+
ASYNC250 Blocking call to `input()` in async context
32+
--> ASYNC250.py:28:5
33+
|
34+
27 | async def main() -> None:
35+
28 | input("sync") # should emit here
36+
| ^^^^^
37+
29 | await asyncio.to_thread(lambda: input("async")) # but not here
38+
|

crates/ruff_python_semantic/src/model.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,8 +1595,13 @@ impl<'a> SemanticModel<'a> {
15951595
/// Return `true` if the model is in an async context.
15961596
pub fn in_async_context(&self) -> bool {
15971597
for scope in self.current_scopes() {
1598-
if let ScopeKind::Function(ast::StmtFunctionDef { is_async, .. }) = scope.kind {
1599-
return *is_async;
1598+
match scope.kind {
1599+
ScopeKind::Class(_) | ScopeKind::Lambda(_) => return false,
1600+
ScopeKind::Function(ast::StmtFunctionDef { is_async, .. }) => return *is_async,
1601+
ScopeKind::Generator { .. }
1602+
| ScopeKind::Module
1603+
| ScopeKind::Type
1604+
| ScopeKind::DunderClassCell => {}
16001605
}
16011606
}
16021607
false

0 commit comments

Comments
 (0)