Skip to content

Commit b0f2018

Browse files
Prevent causing a false negative
1 parent 5f9e378 commit b0f2018

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

pylint/checkers/variables.py

+7
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,13 @@ def _uncertain_nodes_in_except_blocks(
698698
and closest_try_except.parent.parent_of(node_statement)
699699
):
700700
uncertain_nodes.append(other_node)
701+
# Or the node_statement is in the else block of the relevant TryExcept
702+
elif (
703+
isinstance(node_statement.parent, nodes.TryExcept)
704+
and node_statement in node_statement.parent.orelse
705+
and closest_try_except.parent.parent_of(node_statement)
706+
):
707+
uncertain_nodes.append(other_node)
701708
# Assume the except blocks execute, so long as each handler
702709
# defines the name, raises, or returns.
703710
elif all(

tests/functional/u/used/used_before_assignment_else_return.py

+11
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,14 @@ def invalid_3():
4848
pass
4949
finally:
5050
print(error) # [used-before-assignment]
51+
52+
53+
def invalid_4():
54+
"""Should not rely on the name in the else even if it returns."""
55+
try:
56+
pass
57+
except ValueError:
58+
error = True
59+
else:
60+
print(error) # [used-before-assignment]
61+
return
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
used-before-assignment:25:14:25:19:invalid:Using variable 'error' before assignment:CONTROL_FLOW
22
used-before-assignment:38:14:38:19:invalid_2:Using variable 'error' before assignment:CONTROL_FLOW
33
used-before-assignment:50:14:50:19:invalid_3:Using variable 'error' before assignment:CONTROL_FLOW
4+
used-before-assignment:60:14:60:19:invalid_4:Using variable 'error' before assignment:CONTROL_FLOW

0 commit comments

Comments
 (0)