Skip to content

Commit 55fed7c

Browse files
Better and faster implementation of instance_has_bool
1 parent 433f73b commit 55fed7c

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

pylint/checkers/refactoring/len_checker.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ def visit_call(self, node):
107107

108108
@staticmethod
109109
def instance_has_bool(class_def: astroid.ClassDef) -> bool:
110-
return any(hasattr(f, "name") and f.name == "__bool__" for f in class_def.body)
110+
try:
111+
class_def.getattr("__bool__")
112+
except astroid.AttributeInferenceError:
113+
...
114+
return False
111115

112116
@utils.check_messages("len-as-condition")
113117
def visit_unaryop(self, node):

tests/functional/l/len_checks.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ class ChildClassWithoutBool(ClassWithoutBool):
120120
pass
121121

122122
assert len(ClassWithBool())
123-
# We could expect to not have a len-as-condition for ChildClassWithBool,
124-
# but I don't think the required analysis is worth it.
125-
assert len(ChildClassWithBool()) # [len-as-condition]
123+
assert len(ChildClassWithBool())
126124
assert len(ClassWithoutBool()) # [len-as-condition]
127125
assert len(ChildClassWithoutBool()) # [len-as-condition]
128126
assert len(range(0)) # [len-as-condition]

tests/functional/l/len_checks.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ len-as-condition:98:github_issue_1331_v3:Do not use `len(SEQUENCE)` without comp
1313
len-as-condition:101:github_issue_1331_v4:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
1414
len-as-condition:103::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
1515
len-as-condition:104::Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
16+
len-as-condition:124:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
1617
len-as-condition:125:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
1718
len-as-condition:126:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
1819
len-as-condition:127:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
1920
len-as-condition:128:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
2021
len-as-condition:129:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
2122
len-as-condition:130:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
2223
len-as-condition:131:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
23-
len-as-condition:132:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
24-
len-as-condition:163:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
24+
len-as-condition:161:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty

0 commit comments

Comments
 (0)