summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfly <fly@users.noreply.github.com>2023-01-12 20:49:18 +0100
committerGitHub <noreply@github.com>2023-01-12 20:49:18 +0100
commit0c5ab132e262280da98742e5cf73ecc1cbfa7377 (patch)
treea9e588c59d3e5f7f7b7d6fe957b07236ab4d5ba5
parente487a4659dcaeb7a48b084b2acbe5274583b1e3a (diff)
downloadpylint-git-0c5ab132e262280da98742e5cf73ecc1cbfa7377.tar.gz
Fixes false positive for `try-except-raise` with multiple exceptions in one except statement if exception are in different namespace (#8052)
Closes #8051
-rw-r--r--doc/whatsnew/fragments/8051.false_positive4
-rw-r--r--pylint/checkers/exceptions.py2
-rw-r--r--tests/functional/t/try_except_raise.py14
-rw-r--r--tests/functional/t/try_except_raise.txt2
4 files changed, 19 insertions, 3 deletions
diff --git a/doc/whatsnew/fragments/8051.false_positive b/doc/whatsnew/fragments/8051.false_positive
new file mode 100644
index 000000000..9fc6c2274
--- /dev/null
+++ b/doc/whatsnew/fragments/8051.false_positive
@@ -0,0 +1,4 @@
+Fixes false positive for ``try-except-raise`` with multiple exceptions in one
+except statement if exception are in different namespace.
+
+Closes #8051
diff --git a/pylint/checkers/exceptions.py b/pylint/checkers/exceptions.py
index a563c6eb9..fbe71a598 100644
--- a/pylint/checkers/exceptions.py
+++ b/pylint/checkers/exceptions.py
@@ -488,7 +488,7 @@ class ExceptionsChecker(checkers.BaseChecker):
{
exception
for exception in exceptions_in_handler.elts
- if isinstance(exception, nodes.Name)
+ if isinstance(exception, (nodes.Name, nodes.Attribute))
}
)
elif exceptions_in_handler:
diff --git a/tests/functional/t/try_except_raise.py b/tests/functional/t/try_except_raise.py
index 006a29bf9..b82a4bba1 100644
--- a/tests/functional/t/try_except_raise.py
+++ b/tests/functional/t/try_except_raise.py
@@ -1,5 +1,5 @@
# pylint:disable=missing-docstring, unreachable, bad-except-order, bare-except, unnecessary-pass
-# pylint: disable=undefined-variable, broad-except, raise-missing-from
+# pylint: disable=undefined-variable, broad-except, raise-missing-from, too-few-public-methods
try:
int("9a")
except: # [try-except-raise]
@@ -81,6 +81,18 @@ except (FileNotFoundError, PermissionError):
except OSError:
print("a failure")
+class NameSpace:
+ error1 = FileNotFoundError
+ error2 = PermissionError
+ parent_error=OSError
+
+try:
+ pass
+except (NameSpace.error1, NameSpace.error2):
+ raise
+except NameSpace.parent_error:
+ print("a failure")
+
# also consider tuples for subsequent exception handler instead of just bare except handler
try:
pass
diff --git a/tests/functional/t/try_except_raise.txt b/tests/functional/t/try_except_raise.txt
index 20181fa7f..7145e07b7 100644
--- a/tests/functional/t/try_except_raise.txt
+++ b/tests/functional/t/try_except_raise.txt
@@ -3,4 +3,4 @@ try-except-raise:16:0:18:29::The except handler raises immediately:UNDEFINED
try-except-raise:53:4:54:13:ddd:The except handler raises immediately:UNDEFINED
try-except-raise:67:0:68:9::The except handler raises immediately:UNDEFINED
try-except-raise:72:0:73:9::The except handler raises immediately:UNDEFINED
-try-except-raise:94:0:95:9::The except handler raises immediately:UNDEFINED
+try-except-raise:106:0:107:9::The except handler raises immediately:UNDEFINED