Skip to content

Commit 9955701

Browse files
Fix 'use-implicit-booleaness' wrongly emitted for generators. (#10107) (#10109)
Co-authored-by: Nedelcu <Nedelcu Ioan-Andrei> Co-authored-by: Pierre Sassoulas <[email protected]> (cherry picked from commit a4e91d7) Co-authored-by: Nedelcu Ioan-Andrei <[email protected]>
1 parent a5a1bc3 commit 9955701

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a false positive for `use-implicit-booleaness-not-len`. No lint should be emitted for
2+
generators (`len` is not defined for generators).
3+
4+
Refs #10100

pylint/checkers/refactoring/implicit_booleaness_checker.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,8 @@ def visit_call(self, node: nodes.Call) -> None:
125125
if not utils.is_test_condition(node, parent):
126126
return
127127
len_arg = node.args[0]
128-
generator_or_comprehension = (
129-
nodes.ListComp,
130-
nodes.SetComp,
131-
nodes.DictComp,
132-
nodes.GeneratorExp,
133-
)
134-
if isinstance(len_arg, generator_or_comprehension):
135-
# The node is a generator or comprehension as in len([x for x in ...])
128+
if isinstance(len_arg, (nodes.ListComp, nodes.SetComp, nodes.DictComp)):
129+
# The node is a comprehension as in len([x for x in ...])
136130
self.add_message(
137131
"use-implicit-booleaness-not-len",
138132
node=node,

tests/functional/u/use/use_implicit_booleaness_not_len.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class ChildClassWithoutBool(ClassWithoutBool):
125125
assert len(ChildClassWithoutBool()) # [use-implicit-booleaness-not-len]
126126
assert len(range(0)) # [use-implicit-booleaness-not-len]
127127
assert len([t + 1 for t in []]) # [use-implicit-booleaness-not-len]
128-
assert len(u + 1 for u in []) # [use-implicit-booleaness-not-len]
128+
assert len(u + 1 for u in []) # Should be fine
129129
assert len({"1":(v + 1) for v in {}}) # [use-implicit-booleaness-not-len]
130130
assert len(set((w + 1) for w in set())) # [use-implicit-booleaness-not-len]
131131

@@ -189,3 +189,7 @@ def github_issue_4215():
189189

190190
if len('TEST'):
191191
pass
192+
193+
def github_issue_10100():
194+
if len((x for x in [1, 2, 3])): # Should be fine
195+
print("yay!")

tests/functional/u/use/use_implicit_booleaness_not_len.txt

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use-implicit-booleaness-not-len:124:11:124:34:github_issue_1879:Do not use `len(
1818
use-implicit-booleaness-not-len:125:11:125:39:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:INFERENCE
1919
use-implicit-booleaness-not-len:126:11:126:24:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:INFERENCE
2020
use-implicit-booleaness-not-len:127:11:127:35:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:HIGH
21-
use-implicit-booleaness-not-len:128:11:128:33:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:HIGH
2221
use-implicit-booleaness-not-len:129:11:129:41:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:HIGH
2322
use-implicit-booleaness-not-len:130:11:130:43:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:INFERENCE
2423
use-implicit-booleaness-not-len:171:11:171:42:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty:INFERENCE

0 commit comments

Comments
 (0)