Skip to content

Commit 4fc721d

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Avoid used-before-assignment in except handlers employing type annotations
1 parent ca10de2 commit 4fc721d

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

doc/whatsnew/2/2.14/full.rst

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ What's New in Pylint 2.14.2?
55
----------------------------
66
Release date: TBA
77

8+
* Fixed a false positive for ``used-before-assignment`` when a try block returns
9+
but an except handler defines a name via type annotation.
10+
811

912

1013
What's New in Pylint 2.14.1?

pylint/checkers/variables.py

+7
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,13 @@ def _uncertain_nodes_in_except_blocks(
726726
def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool:
727727
if isinstance(node, (nodes.Raise, nodes.Return)):
728728
return True
729+
if (
730+
isinstance(node, nodes.AnnAssign)
731+
and node.value
732+
and isinstance(node.target, nodes.AssignName)
733+
and node.target.name == name
734+
):
735+
return True
729736
if isinstance(node, nodes.Assign):
730737
for target in node.targets:
731738
for elt in utils.get_all_elements(target):

tests/functional/u/used/used_before_assignment_except_handler_for_try_with_return.py

+10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def func_ok7(var):
9999
print(msg)
100100

101101

102+
def func_ok8(var):
103+
"""Define 'msg' in one handler via type annotation."""
104+
try:
105+
return 1 / var.some_other_func()
106+
except ZeroDivisionError:
107+
# See func_invalid2() for mere annotation without value
108+
msg: str = "Division by 0"
109+
print(msg)
110+
111+
102112
def func_invalid1(var):
103113
"""'msg' is not defined in one handler."""
104114
try:
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
used-before-assignment:16:14:16:29:function:Using variable 'failure_message' before assignment:CONTROL_FLOW
2-
used-before-assignment:110:10:110:13:func_invalid1:Using variable 'msg' before assignment:CONTROL_FLOW
3-
used-before-assignment:121:10:121:13:func_invalid2:Using variable 'msg' before assignment:CONTROL_FLOW
4-
used-before-assignment:140:10:140:13:func_invalid3:Using variable 'msg' before assignment:CONTROL_FLOW
5-
used-before-assignment:153:10:153:13:func_invalid4:Using variable 'msg' before assignment:CONTROL_FLOW
6-
used-before-assignment:165:10:165:13:func_invalid5:Using variable 'msg' before assignment:CONTROL_FLOW
7-
used-before-assignment:177:10:177:13:func_invalid6:Using variable 'msg' before assignment:CONTROL_FLOW
2+
used-before-assignment:120:10:120:13:func_invalid1:Using variable 'msg' before assignment:CONTROL_FLOW
3+
used-before-assignment:131:10:131:13:func_invalid2:Using variable 'msg' before assignment:CONTROL_FLOW
4+
used-before-assignment:150:10:150:13:func_invalid3:Using variable 'msg' before assignment:CONTROL_FLOW
5+
used-before-assignment:163:10:163:13:func_invalid4:Using variable 'msg' before assignment:CONTROL_FLOW
6+
used-before-assignment:175:10:175:13:func_invalid5:Using variable 'msg' before assignment:CONTROL_FLOW
7+
used-before-assignment:187:10:187:13:func_invalid6:Using variable 'msg' before assignment:CONTROL_FLOW

0 commit comments

Comments
 (0)