diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-05-03 10:34:08 +0300 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-05-03 10:34:08 +0300 |
commit | 835c84ba6ec8a46875f447a7c701e5c356a6266d (patch) | |
tree | 3785bae0ecd1801917744f19b980e7d1db243073 /pylint/checkers/classes.py | |
parent | 792fe2e85a458c476040bf06c1da65082190ff8c (diff) | |
download | pylint-git-835c84ba6ec8a46875f447a7c701e5c356a6266d.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.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/pylint/checkers/classes.py b/pylint/checkers/classes.py index 080abd97c..f957335c7 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): |