Skip to content

Commit fe547fb

Browse files
authored
Add additional test cases used-before-assignment with try-except (#5523)
1 parent 623b54d commit fe547fb

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
try blocks with return statements.
33
See: https://github.com/PyCQA/pylint/issues/5500.
44
"""
5+
# pylint: disable=inconsistent-return-statements
6+
7+
58
def function():
69
"""Assume except blocks execute if the try block returns."""
710
try:
@@ -13,3 +16,36 @@ def function():
1316
print(failure_message) # [used-before-assignment]
1417

1518
return failure_message
19+
20+
21+
def func_ok(var):
22+
"""'msg' is defined in all ExceptHandlers."""
23+
try:
24+
return 1 / var.some_other_func()
25+
except AttributeError:
26+
msg = "Attribute not defined"
27+
except ZeroDivisionError:
28+
msg = "Devision by 0"
29+
print(msg)
30+
31+
32+
def func_ok2(var):
33+
"""'msg' is defined in all ExceptHandlers that don't raise an Exception."""
34+
try:
35+
return 1 / var.some_other_func()
36+
except AttributeError as ex:
37+
raise Exception from ex
38+
except ZeroDivisionError:
39+
msg = "Devision by 0"
40+
print(msg)
41+
42+
43+
def func_ok3(var):
44+
"""'msg' is defined in all ExceptHandlers that don't return."""
45+
try:
46+
return 1 / var.some_other_func()
47+
except AttributeError:
48+
return
49+
except ZeroDivisionError:
50+
msg = "Devision by 0"
51+
print(msg)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
used-before-assignment:13:14:13:29:function:Using variable 'failure_message' before assignment:UNDEFINED
1+
used-before-assignment:16:14:16:29:function:Using variable 'failure_message' before assignment:UNDEFINED

0 commit comments

Comments
 (0)