-
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.
Restore existing bindings when unbinding caught exceptions (#5256)
## Summary In the latest release, we made some improvements to the semantic model, but our modifications to exception-unbinding are causing some false-positives. For example: ```py try: v = 3 except ImportError as v: print(v) else: print(v) ``` In the latest release, we started unbinding `v` after the `except` handler. (We used to restore the existing binding, the `v = 3`, but this was quite complicated.) Because we don't have full branch analysis, we can't then know that `v` is still bound in the `else` branch. The solution here modifies `resolve_read` to skip-lookup when hitting unbound exceptions. So when store the "unbind" for `except ImportError as v`, we save the binding that it shadowed `v = 3`, and skip to that. Closes #5249. Closes #5250.
- Loading branch information
1 parent
d99b3bf
commit ecf61d4
Showing
13 changed files
with
429 additions
and
25 deletions.
There are no files selected for viewing
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
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
44 changes: 44 additions & 0 deletions
44
...napshots/ruff__rules__pyflakes__tests__load_after_multiple_unbinds_from_module_scope.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,44 @@ | ||
--- | ||
source: crates/ruff/src/rules/pyflakes/mod.rs | ||
--- | ||
<filename>:7:26: F841 [*] Local variable `x` is assigned to but never used | ||
| | ||
5 | try: | ||
6 | pass | ||
7 | except ValueError as x: | ||
| ^ F841 | ||
8 | pass | ||
| | ||
= help: Remove assignment to unused variable `x` | ||
|
||
ℹ Fix | ||
4 4 | def f(): | ||
5 5 | try: | ||
6 6 | pass | ||
7 |- except ValueError as x: | ||
7 |+ except ValueError: | ||
8 8 | pass | ||
9 9 | | ||
10 10 | try: | ||
|
||
<filename>:12:26: F841 [*] Local variable `x` is assigned to but never used | ||
| | ||
10 | try: | ||
11 | pass | ||
12 | except ValueError as x: | ||
| ^ F841 | ||
13 | pass | ||
| | ||
= help: Remove assignment to unused variable `x` | ||
|
||
ℹ Fix | ||
9 9 | | ||
10 10 | try: | ||
11 11 | pass | ||
12 |- except ValueError as x: | ||
12 |+ except ValueError: | ||
13 13 | pass | ||
14 14 | | ||
15 15 | # This should resolve to the `x` in `x = 1`. | ||
|
||
|
32 changes: 32 additions & 0 deletions
32
.../pyflakes/snapshots/ruff__rules__pyflakes__tests__load_after_unbind_from_class_scope.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,32 @@ | ||
--- | ||
source: crates/ruff/src/rules/pyflakes/mod.rs | ||
--- | ||
<filename>:8:30: F841 [*] Local variable `x` is assigned to but never used | ||
| | ||
6 | try: | ||
7 | pass | ||
8 | except ValueError as x: | ||
| ^ F841 | ||
9 | pass | ||
| | ||
= help: Remove assignment to unused variable `x` | ||
|
||
ℹ Fix | ||
5 5 | def f(): | ||
6 6 | try: | ||
7 7 | pass | ||
8 |- except ValueError as x: | ||
8 |+ except ValueError: | ||
9 9 | pass | ||
10 10 | | ||
11 11 | # This should raise an F821 error, rather than resolving to the | ||
|
||
<filename>:13:15: F821 Undefined name `x` | ||
| | ||
11 | # This should raise an F821 error, rather than resolving to the | ||
12 | # `x` in `x = 1`. | ||
13 | print(x) | ||
| ^ F821 | ||
| | ||
|
||
|
24 changes: 24 additions & 0 deletions
24
...pyflakes/snapshots/ruff__rules__pyflakes__tests__load_after_unbind_from_module_scope.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,24 @@ | ||
--- | ||
source: crates/ruff/src/rules/pyflakes/mod.rs | ||
--- | ||
<filename>:7:26: F841 [*] Local variable `x` is assigned to but never used | ||
| | ||
5 | try: | ||
6 | pass | ||
7 | except ValueError as x: | ||
| ^ F841 | ||
8 | pass | ||
| | ||
= help: Remove assignment to unused variable `x` | ||
|
||
ℹ Fix | ||
4 4 | def f(): | ||
5 5 | try: | ||
6 6 | pass | ||
7 |- except ValueError as x: | ||
7 |+ except ValueError: | ||
8 8 | pass | ||
9 9 | | ||
10 10 | # This should resolve to the `x` in `x = 1`. | ||
|
||
|
44 changes: 44 additions & 0 deletions
44
...s/snapshots/ruff__rules__pyflakes__tests__load_after_unbind_from_nested_module_scope.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,44 @@ | ||
--- | ||
source: crates/ruff/src/rules/pyflakes/mod.rs | ||
--- | ||
<filename>:7:26: F841 [*] Local variable `x` is assigned to but never used | ||
| | ||
5 | try: | ||
6 | pass | ||
7 | except ValueError as x: | ||
| ^ F841 | ||
8 | pass | ||
| | ||
= help: Remove assignment to unused variable `x` | ||
|
||
ℹ Fix | ||
4 4 | def f(): | ||
5 5 | try: | ||
6 6 | pass | ||
7 |- except ValueError as x: | ||
7 |+ except ValueError: | ||
8 8 | pass | ||
9 9 | | ||
10 10 | def g(): | ||
|
||
<filename>:13:30: F841 [*] Local variable `x` is assigned to but never used | ||
| | ||
11 | try: | ||
12 | pass | ||
13 | except ValueError as x: | ||
| ^ F841 | ||
14 | pass | ||
| | ||
= help: Remove assignment to unused variable `x` | ||
|
||
ℹ Fix | ||
10 10 | def g(): | ||
11 11 | try: | ||
12 12 | pass | ||
13 |- except ValueError as x: | ||
13 |+ except ValueError: | ||
14 14 | pass | ||
15 15 | | ||
16 16 | # This should resolve to the `x` in `x = 1`. | ||
|
||
|
45 changes: 45 additions & 0 deletions
45
.../snapshots/ruff__rules__pyflakes__tests__print_in_body_after_double_shadowing_except.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,45 @@ | ||
--- | ||
source: crates/ruff/src/rules/pyflakes/mod.rs | ||
--- | ||
<filename>:7:26: F841 [*] Local variable `x` is assigned to but never used | ||
| | ||
5 | try: | ||
6 | 1 / 0 | ||
7 | except ValueError as x: | ||
| ^ F841 | ||
8 | pass | ||
9 | except ImportError as x: | ||
| | ||
= help: Remove assignment to unused variable `x` | ||
|
||
ℹ Fix | ||
4 4 | | ||
5 5 | try: | ||
6 6 | 1 / 0 | ||
7 |- except ValueError as x: | ||
7 |+ except ValueError: | ||
8 8 | pass | ||
9 9 | except ImportError as x: | ||
10 10 | pass | ||
|
||
<filename>:9:27: F841 [*] Local variable `x` is assigned to but never used | ||
| | ||
7 | except ValueError as x: | ||
8 | pass | ||
9 | except ImportError as x: | ||
| ^ F841 | ||
10 | pass | ||
| | ||
= help: Remove assignment to unused variable `x` | ||
|
||
ℹ Fix | ||
6 6 | 1 / 0 | ||
7 7 | except ValueError as x: | ||
8 8 | pass | ||
9 |- except ImportError as x: | ||
9 |+ except ImportError: | ||
10 10 | pass | ||
11 11 | | ||
12 12 | # No error here, though it should arguably be an F821 error. `x` will | ||
|
||
|
Oops, something went wrong.