diff options
Diffstat (limited to 'pylint/checkers/base.py')
-rw-r--r-- | pylint/checkers/base.py | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index 2e5613a..ac86d44 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -1446,8 +1446,9 @@ class ComparisonChecker(_BasicChecker): """checks for singleton comparison and for yoda condition - singleton comparison: 'expr == True', 'expr == False' and 'expr == None' - - yoda condition: 'const "comp" var' where comp can be '==', '!=', '<', - '<=', '>' or '>=' + - yoda condition: 'const "comp" right' where comp can be '==', '!=', '<', + '<=', '>' or '>=', and right can be a variable, an attribute, a method or + a function """ msgs = {'C0121': ('Comparison to %s should be %s', 'singleton-comparison', @@ -1475,16 +1476,18 @@ class ComparisonChecker(_BasicChecker): node=root_node, args=(None, "'expr is None'")) - def _warn_misplaced_constant(self, node, left, right, operator): - if isinstance(right, astroid.Name): + def _check_misplaced_constant(self, node, left, right, operator): + suggestion = None + if isinstance(right, (astroid.Name, astroid.Attribute, astroid.Call)): reverse_op = {'<': '>', '<=': '>=', '>': '<', '>=': '<='} if operator in reverse_op: operator = reverse_op[operator] - suggestion = '%s %s %s' % (right.name, operator, left.value) - else: + suggestion = '%s %s %s' % (right.as_string(), operator, left.value) + elif isinstance(right, astroid.Const): suggestion = 'between a variable and a constant' - self.add_message('misplaced-comparison-constant', node=node, - args=(suggestion,)) + if suggestion: + self.add_message('misplaced-comparison-constant', node=node, + args=(suggestion,)) @check_messages('singleton-comparison', 'misplaced-comparison-constant') def visit_compare(self, node): @@ -1497,13 +1500,13 @@ class ComparisonChecker(_BasicChecker): operator, right = node.ops[0] if operator == '==': if isinstance(left, astroid.Const): - self._warn_misplaced_constant(node, left, right, operator) + self._check_misplaced_constant(node, left, right, operator) self.check_singleton_comparison(left, node) elif isinstance(right, astroid.Const): self.check_singleton_comparison(right, node) elif (operator in ('<', '<=', '>', '>=', '!=') and isinstance(left, astroid.Const)): - self._warn_misplaced_constant(node, left, right, operator) + self._check_misplaced_constant(node, left, right, operator) def register(linter): |