diff --git a/doc/whatsnew/fragments/10100.false_positive b/doc/whatsnew/fragments/10100.false_positive new file mode 100644 index 0000000000..2f3572bf55 --- /dev/null +++ b/doc/whatsnew/fragments/10100.false_positive @@ -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 diff --git a/pylint/checkers/refactoring/implicit_booleaness_checker.py b/pylint/checkers/refactoring/implicit_booleaness_checker.py index 4d3d47cf9f..7522faeb1c 100644 --- a/pylint/checkers/refactoring/implicit_booleaness_checker.py +++ b/pylint/checkers/refactoring/implicit_booleaness_checker.py @@ -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, diff --git a/tests/functional/u/use/use_implicit_booleaness_not_len.py b/tests/functional/u/use/use_implicit_booleaness_not_len.py index 1261aa3014..4a1063e757 100644 --- a/tests/functional/u/use/use_implicit_booleaness_not_len.py +++ b/tests/functional/u/use/use_implicit_booleaness_not_len.py @@ -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] @@ -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!") diff --git a/tests/functional/u/use/use_implicit_booleaness_not_len.txt b/tests/functional/u/use/use_implicit_booleaness_not_len.txt index 85917de828..868e746bfb 100644 --- a/tests/functional/u/use/use_implicit_booleaness_not_len.txt +++ b/tests/functional/u/use/use_implicit_booleaness_not_len.txt @@ -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