-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow bindings to be created and referenced within annotations (#7885)
## Summary Given: ```python baz: Annotated[ str, [qux for qux in foo], ] ``` We treat `baz` as `BindingKind::Annotation`, to ensure that references to `baz` are marked as unbound. However, we were _also_ treating `qux` as `BindingKind::Annotation`, which meant that the load in the comprehension _also_ errored. Closes #7879.
- Loading branch information
1 parent
ec7395b
commit a3e8e77
Showing
6 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
crates/ruff_linter/resources/test/fixtures/pyflakes/F821_18.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Test bindings created within annotations.""" | ||
|
||
from typing import Annotated | ||
|
||
foo = [1, 2, 3, 4, 5] | ||
|
||
|
||
class Bar: | ||
# OK: Allow list comprehensions in annotations (i.e., treat `qux` as a valid | ||
# load in the scope of the annotation). | ||
baz: Annotated[ | ||
str, | ||
[qux for qux in foo], | ||
] | ||
|
||
|
||
# OK: Allow named expressions in annotations. | ||
x: (y := 1) | ||
print(y) |
21 changes: 21 additions & 0 deletions
21
crates/ruff_linter/resources/test/fixtures/pyflakes/F821_19.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"""Test bindings created within annotations under `__future__` annotations.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Annotated | ||
|
||
foo = [1, 2, 3, 4, 5] | ||
|
||
|
||
class Bar: | ||
# OK: Allow list comprehensions in annotations (i.e., treat `qux` as a valid | ||
# load in the scope of the annotation). | ||
baz: Annotated[ | ||
str, | ||
[qux for qux in foo], | ||
] | ||
|
||
|
||
# Error: `y` is not defined. | ||
x: (y := 1) | ||
print(y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...er/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_18.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
--- | ||
|
12 changes: 12 additions & 0 deletions
12
...er/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_19.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
--- | ||
F821_19.py:21:7: F821 Undefined name `y` | ||
| | ||
19 | # Error: `y` is not defined. | ||
20 | x: (y := 1) | ||
21 | print(y) | ||
| ^ F821 | ||
| | ||
|
||
|