Skip to content

Commit

Permalink
Fix 'use-implicit-booleaness' wrongly emitted for generators. (#10107) (
Browse files Browse the repository at this point in the history
#10109)

Co-authored-by: Nedelcu <Nedelcu Ioan-Andrei>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
(cherry picked from commit a4e91d7)

Co-authored-by: Nedelcu Ioan-Andrei <138256980+nedelcu-ioan@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and nedelcu-ioan authored Dec 4, 2024
1 parent a5a1bc3 commit 9955701
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/10100.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a false positive for `use-implicit-booleaness-not-len`. No lint should be emitted for
generators (`len` is not defined for generators).

Refs #10100
10 changes: 2 additions & 8 deletions pylint/checkers/refactoring/implicit_booleaness_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,8 @@ def visit_call(self, node: nodes.Call) -> None:
if not utils.is_test_condition(node, parent):
return
len_arg = node.args[0]
generator_or_comprehension = (
nodes.ListComp,
nodes.SetComp,
nodes.DictComp,
nodes.GeneratorExp,
)
if isinstance(len_arg, generator_or_comprehension):
# The node is a generator or comprehension as in len([x for x in ...])
if isinstance(len_arg, (nodes.ListComp, nodes.SetComp, nodes.DictComp)):
# The node is a comprehension as in len([x for x in ...])
self.add_message(
"use-implicit-booleaness-not-len",
node=node,
Expand Down
6 changes: 5 additions & 1 deletion tests/functional/u/use/use_implicit_booleaness_not_len.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class ChildClassWithoutBool(ClassWithoutBool):
assert len(ChildClassWithoutBool()) # [use-implicit-booleaness-not-len]
assert len(range(0)) # [use-implicit-booleaness-not-len]
assert len([t + 1 for t in []]) # [use-implicit-booleaness-not-len]
assert len(u + 1 for u in []) # [use-implicit-booleaness-not-len]
assert len(u + 1 for u in []) # Should be fine
assert len({"1":(v + 1) for v in {}}) # [use-implicit-booleaness-not-len]
assert len(set((w + 1) for w in set())) # [use-implicit-booleaness-not-len]

Expand Down Expand Up @@ -189,3 +189,7 @@ def github_issue_4215():

if len('TEST'):
pass

def github_issue_10100():
if len((x for x in [1, 2, 3])): # Should be fine
print("yay!")
1 change: 0 additions & 1 deletion tests/functional/u/use/use_implicit_booleaness_not_len.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use-implicit-booleaness-not-len:124:11:124:34:github_issue_1879:Do not use `len(
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
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
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
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
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
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
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
Expand Down

0 comments on commit 9955701

Please sign in to comment.