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 @@ -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
12 changes: 1 addition & 11 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
9 changes: 7 additions & 2 deletions crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down