-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[used-before-assignment] Fix FP for try/else/continue (#9374)
- Loading branch information
1 parent
81bd39a
commit 7d0c7ac
Showing
4 changed files
with
58 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
``used-before-assignment`` is no longer emitted when using a name in a loop and | ||
depending on an earlier name assignment in an ``except`` block paired with | ||
``else: continue``. | ||
|
||
Closes #6804 |
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
42 changes: 42 additions & 0 deletions
42
tests/functional/u/used/used_before_assignment_else_continue.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,42 @@ | ||
"""If the else block continues, it is generally safe to rely on assignments in the except, | ||
inside the same for loop only.""" | ||
|
||
|
||
def safe(): | ||
"""Name used safely inside the loop.""" | ||
while True: | ||
try: | ||
pass | ||
except ValueError: | ||
error = True | ||
else: | ||
continue | ||
|
||
print(error) | ||
|
||
|
||
def halfway_safe(): | ||
"""Name used safely inside the loop, unsafely outside it.""" | ||
for _temp in range(0, 1): | ||
try: | ||
pass | ||
except ValueError: | ||
error = True | ||
else: | ||
continue | ||
|
||
print(error) | ||
print(error) # https://github.com/pylint-dev/pylint/issues/9379 | ||
|
||
|
||
def unsafe(): | ||
"""Name used unsafely outside the loop.""" | ||
for _temp in range(0, 1): | ||
try: | ||
pass | ||
except ValueError: | ||
error = True | ||
else: | ||
continue | ||
|
||
print(error) # [used-before-assignment] |
1 change: 1 addition & 0 deletions
1
tests/functional/u/used/used_before_assignment_else_continue.txt
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 @@ | ||
used-before-assignment:42:10:42:15:unsafe:Using variable 'error' before assignment:CONTROL_FLOW |