Skip to content

Commit 3477e00

Browse files
Fix pylint-dev#5500: Fix false-positive used-before-assignment for assignments in except blocks following try blocks that return
1 parent 736c0be commit 3477e00

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

ChangeLog

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ Release date: TBA
1111
..
1212
Put new features here and also in 'doc/whatsnew/2.13.rst'
1313

14+
* ``used-before-assignment`` now considers that assignments in a try block
15+
may not have occurred when the except or finally blocks are executed.
16+
17+
Closes #85, #2615
18+
1419
* ``used-before-assignment`` now assumes that assignments in except blocks
1520
may not have occurred and warns accordingly.
1621

1722
Closes #4761
1823

19-
* ``used-before-assignment`` now considers that assignments in a try block
20-
may not have occurred when the except or finally blocks are executed.
24+
* When evaluating statements after an except block, ``used-before-assignment``
25+
assumes that assignments in the except blocks took place if the
26+
corresponding try block contained a return statement.
2127

22-
Closes #85, #2615
28+
Closes #5500
2329

2430
* ``used-before-assignment`` now checks names in try blocks.
2531

doc/whatsnew/2.13.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ Other Changes
4343

4444
Closes #5323
4545

46+
* ``used-before-assignment`` now considers that assignments in a try block
47+
may not have occurred when the except or finally blocks are executed.
48+
49+
Closes #85, #2615
50+
4651
* ``used-before-assignment`` now assumes that assignments in except blocks
4752
may not have occurred and warns accordingly.
4853

4954
Closes #4761
5055

51-
* ``used-before-assignment`` now considers that assignments in a try block
52-
may not have occurred when the except or finally blocks are executed.
56+
* When evaluating statements after an except block, ``used-before-assignment``
57+
assumes that assignments in the except blocks took place if the
58+
corresponding try block contained a return statement.
5359

54-
Closes #85, #2615
60+
Closes #5500
5561

5662
* ``used-before-assignment`` now checks names in try blocks.
5763

pylint/checkers/variables.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,12 @@ def get_next_to_consume(self, node):
656656
and isinstance(
657657
n.statement(future=True).parent.parent, nodes.TryExcept
658658
)
659+
# If the try block returns we assume that assignments in the except
660+
# handlers could have happened.
661+
and not any(
662+
isinstance(try_statement, nodes.Return)
663+
for try_statement in n.statement(future=True).parent.parent.body
664+
)
659665
)
660666
or n.statement(future=True).parent.parent_of(node)
661667
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""used-before-assignment (E0601)"""
2+
def function():
3+
"""Assume except blocks execute if the try block returns."""
4+
try:
5+
return "success message"
6+
except ValueError:
7+
failure_message = "failure message"
8+
9+
return failure_message

0 commit comments

Comments
 (0)