summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSushobhit <31987769+sushobhit27@users.noreply.github.com>2018-09-28 17:36:20 +0530
committerClaudiu Popa <pcmanticore@gmail.com>2018-09-28 14:06:20 +0200
commit47c258eee17937bae776594a101eb8fdd36cd71d (patch)
tree37307b532b79958c85cbc6a8b3fc1fe69002723e
parent20694325dde16377dd5e295ccdd82dffa68ce7aa (diff)
downloadpylint-git-47c258eee17937bae776594a101eb8fdd36cd71d.tar.gz
Fix utils.is_subclass_of to be aware of complex hierarchies (#2508)
-rw-r--r--ChangeLog4
-rw-r--r--doc/whatsnew/2.2.rst4
-rw-r--r--pylint/checkers/utils.py18
-rw-r--r--pylint/test/functional/try_except_raise.py17
4 files changed, 33 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index 403662c3f..5e27fcc82 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,6 +15,10 @@ Release date: TBA
Close #2513
+ * `try-except-raise` checker now handles multilevel inheritance hirerachy for exceptions correctly.
+
+ Close #2484
+
* Add a new check, ``simplifiable-if-expression`` for expressions like ``True if cond else False``.
Close #2487
diff --git a/doc/whatsnew/2.2.rst b/doc/whatsnew/2.2.rst
index 32053fb24..09176daad 100644
--- a/doc/whatsnew/2.2.rst
+++ b/doc/whatsnew/2.2.rst
@@ -20,6 +20,10 @@ New checkers
Other Changes
=============
+* `try-except-raise` checker now handles multilevel inheritance hirerachy for exceptions correctly.
+
+ Close #2484
+
* Ignore import x.y.z as z cases for checker `useless-import-alias`.
* `unnecessary-pass` is now also emitted when a function or class contains only docstring and pass statement,
diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py
index 59277c2e3..f4d10006b 100644
--- a/pylint/checkers/utils.py
+++ b/pylint/checkers/utils.py
@@ -1125,17 +1125,17 @@ def is_postponed_evaluation_enabled(node: astroid.node_classes.NodeNG) -> bool:
)
-def is_subclass_of(node_a: astroid.ClassDef, node_b: astroid.ClassDef) -> bool:
+def is_subclass_of(child: astroid.ClassDef, parent: astroid.ClassDef) -> bool:
"""
Check if first node is a subclass of second node.
- :param node_a: Node to check for subclass.
- :type node_a: astroid.ClassDef
- :param node_b: Node to check for superclass.
- :type node_b: astroid.ClassDef
- :returns: True if node_a is derived from node_b. False otherwise.
- :rtype: bool
+ :param child: Node to check for subclass.
+ :param parent: Node to check for superclass.
+ :returns: True if child is derived from parent. False otherwise.
"""
- if not all(isinstance(node, astroid.ClassDef) for node in (node_a, node_b)):
+ if not all(isinstance(node, astroid.ClassDef) for node in (child, parent)):
return False
- return node_b.name in {base.name for base in node_a.bases}
+ for ancestor in child.ancestors():
+ if astroid.helpers.is_subtype(ancestor, parent):
+ return True
+ return False
diff --git a/pylint/test/functional/try_except_raise.py b/pylint/test/functional/try_except_raise.py
index 033681e35..2a9bed799 100644
--- a/pylint/test/functional/try_except_raise.py
+++ b/pylint/test/functional/try_except_raise.py
@@ -1,5 +1,5 @@
# pylint:disable=missing-docstring, unreachable, bad-except-order, bare-except, unnecessary-pass
-# pylint: disable=undefined-variable
+# pylint: disable=undefined-variable, broad-except
try:
int("9a")
except: # [try-except-raise]
@@ -103,3 +103,18 @@ except invalid_name: # [try-except-raise]
raise
except TypeError:
pass
+
+try:
+ pass
+except (FileNotFoundError, PermissionError):
+ raise
+except Exception:
+ print("a failure")
+
+try:
+ pass
+except (FileNotFoundError, PermissionError):
+ raise
+except (Exception,):
+ print("a failure")
+ \ No newline at end of file