summaryrefslogtreecommitdiff
path: root/pylint/checkers/classes.py
diff options
context:
space:
mode:
authorClaudiu Popa <cpopa@cloudbasesolutions.com>2015-05-03 10:34:08 +0300
committerClaudiu Popa <cpopa@cloudbasesolutions.com>2015-05-03 10:34:08 +0300
commitd49fab20cbb8509a42b0a0a573cbf2c3ade6f7c8 (patch)
tree4e9d182d7f34bfaab8214834f6d24d87c7ff56ae /pylint/checkers/classes.py
parent351de4bd69d661f5fdd9391d94fe25e748de4322 (diff)
downloadpylint-d49fab20cbb8509a42b0a0a573cbf2c3ade6f7c8.tar.gz
Add two new warnings, duplicate-bases and inconsistent-mro.
duplicate-bases is emitted when a class has the same bases listed more than once in its bases definition, while inconsistent-mro is emitted when no sane mro hierarchy can be determined. Closes issue #526.
Diffstat (limited to 'pylint/checkers/classes.py')
-rw-r--r--pylint/checkers/classes.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/pylint/checkers/classes.py b/pylint/checkers/classes.py
index 080abd9..f957335 100644
--- a/pylint/checkers/classes.py
+++ b/pylint/checkers/classes.py
@@ -23,6 +23,7 @@ from collections import defaultdict
import astroid
from astroid import YES, Instance, are_exclusive, AssAttr, Class
from astroid.bases import Generator, BUILTINS
+from astroid.exceptions import InconsistentMroError, DuplicateBasesError
from astroid.inference import InferenceContext
from pylint.interfaces import IAstroidChecker
@@ -236,7 +237,12 @@ MSGS = {
'inherit-non-class',
'Used when a class inherits from something which is not a '
'class.'),
-
+ 'E0240': ('Inconsistent method resolution order for class %r',
+ 'inconsistent-mro',
+ 'Used when a class has an inconsistent method resolutin order.'),
+ 'E0241': ('Duplicate bases for class %r',
+ 'duplicate-bases',
+ 'Used when a class has duplicate bases.'),
}
@@ -328,6 +334,20 @@ a metaclass class method.'}
self.add_message('no-init', args=node, node=node)
self._check_slots(node)
self._check_proper_bases(node)
+ self._check_consistent_mro(node)
+
+ @check_messages('inconsistent-mro', 'duplicate-bases')
+ def _check_consistent_mro(self, node):
+ """Detect that a class has a consistent mro or duplicate bases."""
+ try:
+ node.mro()
+ except InconsistentMroError:
+ self.add_message('inconsistent-mro', args=node.name, node=node)
+ except DuplicateBasesError:
+ self.add_message('duplicate-bases', args=node.name, node=node)
+ except NotImplementedError:
+ # Old style class, there's no mro so don't do anything.
+ pass
@check_messages('inherit-non-class')
def _check_proper_bases(self, node):