Skip to content

Commit

Permalink
Better and faster implementation of instance_has_bool
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Sep 14, 2020
1 parent 433f73b commit 55fed7c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pylint/checkers/refactoring/len_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ def visit_call(self, 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)
try:
class_def.getattr("__bool__")
except astroid.AttributeInferenceError:
...
return False

@utils.check_messages("len-as-condition")
def visit_unaryop(self, node):
Expand Down
4 changes: 1 addition & 3 deletions tests/functional/l/len_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ class ChildClassWithoutBool(ClassWithoutBool):
pass

assert len(ClassWithBool())
# We could expect to not have a len-as-condition for ChildClassWithBool,
# but I don't think the required analysis is worth it.
assert len(ChildClassWithBool()) # [len-as-condition]
assert len(ChildClassWithBool())
assert len(ClassWithoutBool()) # [len-as-condition]
assert len(ChildClassWithoutBool()) # [len-as-condition]
assert len(range(0)) # [len-as-condition]
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/l/len_checks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ len-as-condition:98:github_issue_1331_v3:Do not use `len(SEQUENCE)` without comp
len-as-condition:101:github_issue_1331_v4:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:103::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:104::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:124:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:125:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:126:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:127:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:128:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:129:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:130:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:131:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:132:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:163:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:161:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty

0 comments on commit 55fed7c

Please sign in to comment.