summaryrefslogtreecommitdiff
path: root/pylint
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2020-09-05 22:23:02 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2020-12-31 11:48:37 +0100
commit8b95fac727db80c5d59057c8c3099d3dafa42c32 (patch)
tree5aa9279f337f99b85d0e9135a30963c2624d9840 /pylint
parentfba1fa626574688521d73628e023b6b9d55198b7 (diff)
downloadpylint-git-8b95fac727db80c5d59057c8c3099d3dafa42c32.tar.gz
Restrict the number of classes affected by len-as-condition
Diffstat (limited to 'pylint')
-rw-r--r--pylint/checkers/refactoring/len_checker.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/pylint/checkers/refactoring/len_checker.py b/pylint/checkers/refactoring/len_checker.py
index 07ccf4ebe..ae4a502d0 100644
--- a/pylint/checkers/refactoring/len_checker.py
+++ b/pylint/checkers/refactoring/len_checker.py
@@ -63,11 +63,20 @@ class LenChecker(checkers.BaseChecker):
parent = node.parent
while isinstance(parent, astroid.BoolOp):
parent = parent.parent
-
# we're finally out of any nested boolean operations so check if
# this len() call is part of a test condition
if utils.is_test_condition(node, parent):
- self.add_message("len-as-condition", node=node)
+ instance = next(node.args[0].infer())
+ mother_classes = self.base_classes_of_node(instance)
+ affected_by_pep8 = any(
+ t in mother_classes for t in ["str", "tuple", "range", "list"]
+ )
+ if affected_by_pep8 and not self.instance_has_bool(instance):
+ self.add_message("len-as-condition", node=node)
+
+ @staticmethod
+ def instance_has_bool(class_def: astroid.ClassDef) -> bool:
+ return any(hasattr(f, "name") and f.name == "__bool__" for f in class_def.body)
@utils.check_messages("len-as-condition")
def visit_unaryop(self, node):