Skip to content

(bugfix) Issue 10100: [C1802] should not be emitted for generators. #10107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading