Skip to content

Add additional test cases used-before-assignment with try-except #5523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
try blocks with return statements.
See: https://github.com/PyCQA/pylint/issues/5500.
"""
# pylint: disable=inconsistent-return-statements


def function():
"""Assume except blocks execute if the try block returns."""
try:
Expand All @@ -13,3 +16,36 @@ def function():
print(failure_message) # [used-before-assignment]

return failure_message


def func_ok(var):
"""'msg' is defined in all ExceptHandlers."""
try:
return 1 / var.some_other_func()
except AttributeError:
msg = "Attribute not defined"
except ZeroDivisionError:
msg = "Devision by 0"
print(msg)


def func_ok2(var):
"""'msg' is defined in all ExceptHandlers that don't raise an Exception."""
try:
return 1 / var.some_other_func()
except AttributeError as ex:
raise Exception from ex
except ZeroDivisionError:
msg = "Devision by 0"
print(msg)


def func_ok3(var):
"""'msg' is defined in all ExceptHandlers that don't return."""
try:
return 1 / var.some_other_func()
except AttributeError:
return
except ZeroDivisionError:
msg = "Devision by 0"
print(msg)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
used-before-assignment:13:14:13:29:function:Using variable 'failure_message' before assignment:UNDEFINED
used-before-assignment:16:14:16:29:function:Using variable 'failure_message' before assignment:UNDEFINED