mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 14:52:01 +00:00
[ty] Don't warn yield
not in function when yield
is in function (#18008)
This commit is contained in:
parent
d37592175f
commit
02fd48132c
7 changed files with 87 additions and 17 deletions
|
@ -9,3 +9,11 @@ class Foo:
|
|||
yield 3
|
||||
yield from 3
|
||||
await f()
|
||||
|
||||
def _():
|
||||
# Invalid yield scopes; but not outside a function
|
||||
type X[T: (yield 1)] = int
|
||||
type Y = (yield 2)
|
||||
|
||||
# Valid yield scope
|
||||
yield 3
|
||||
|
|
|
@ -681,6 +681,17 @@ impl SemanticSyntaxContext for Checker<'_> {
|
|||
false
|
||||
}
|
||||
|
||||
fn in_yield_allowed_context(&self) -> bool {
|
||||
for scope in self.semantic.current_scopes() {
|
||||
match scope.kind {
|
||||
ScopeKind::Class(_) | ScopeKind::Generator { .. } => return false,
|
||||
ScopeKind::Function(_) | ScopeKind::Lambda(_) => return true,
|
||||
ScopeKind::Module | ScopeKind::Type => {}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn in_sync_comprehension(&self) -> bool {
|
||||
for scope in self.semantic.current_scopes() {
|
||||
if let ScopeKind::Generator {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
|
||||
snapshot_kind: text
|
||||
---
|
||||
F704.py:6:5: F704 `yield` statement outside of a function
|
||||
|
|
||||
|
@ -31,4 +30,6 @@ F704.py:11:1: F704 `await` statement outside of a function
|
|||
10 | yield from 3
|
||||
11 | await f()
|
||||
| ^^^^^^^^^ F704
|
||||
12 |
|
||||
13 | def _():
|
||||
|
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue