Skip to content

Commit 8d35557

Browse files
committed
Recurse into all the ancestors when checking if an object is an exception
Since we were going only into the first level, we weren't inferring when a class used a metaclass which defined a base Exception class for the aforementioned class.
1 parent ad39b60 commit 8d35557

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

pylint/checkers/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def inherit_from_std_ex(node):
430430
and node.root().name == EXCEPTIONS_MODULE:
431431
return True
432432
return any(inherit_from_std_ex(parent)
433-
for parent in node.ancestors(recurs=False))
433+
for parent in node.ancestors(recurs=True))
434434

435435
def error_of_type(handler, error_type):
436436
"""

pylint/test/functional/invalid_exceptions_caught.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
"""Test for catching non-exceptions."""
1+
# pylint: disable=missing-docstring, too-few-public-methods
22
# pylint: disable=too-many-ancestors, no-absolute-import, import-error, multiple-imports,wrong-import-position
33
from __future__ import print_function
44

5-
import socket, binascii
5+
import socket, binascii, abc, six
66

77
class MyException(object):
88
"""Custom 'exception'."""
@@ -107,3 +107,17 @@ class SecondSkipException(SkipException):
107107
1 + 42
108108
except range: # [catching-non-exception]
109109
print('caught')
110+
111+
112+
class HasErrorInMRO(six.with_metaclass(abc.ABCMeta, Exception)):
113+
pass
114+
115+
116+
class Second(HasErrorInMRO):
117+
pass
118+
119+
120+
try:
121+
raise Second
122+
except Second:
123+
pass

0 commit comments

Comments
 (0)