summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Mueller <30130371+cdce8p@users.noreply.github.com>2021-12-15 14:37:08 +0100
committerGitHub <noreply@github.com>2021-12-15 14:37:08 +0100
commitfe547fbc47d9f8389260d38d4237b1b003722035 (patch)
tree80f5927b8ec8d55e75de15e56d1a9a56122bcb79
parent623b54d94764b0c51f328c084dfaf72fb2d16457 (diff)
downloadpylint-git-fe547fbc47d9f8389260d38d4237b1b003722035.tar.gz
Add additional test cases used-before-assignment with try-except (#5523)
-rw-r--r--tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.py36
-rw-r--r--tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.txt2
2 files changed, 37 insertions, 1 deletions
diff --git a/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.py b/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.py
index d976a46b2..f915a85cc 100644
--- a/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.py
+++ b/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.py
@@ -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:
@@ -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)
diff --git a/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.txt b/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.txt
index e5d8d3de8..7ca99f115 100644
--- a/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.txt
+++ b/tests/functional/u/use/used_before_assignment_except_handler_for_try_with_return.txt
@@ -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