Skip to content
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

use-implicit-booleaness-not-len [C1802] should not be emitted for generators #10100

Closed
dylwil3 opened this issue Nov 25, 2024 · 2 comments
Closed
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Good first issue Friendly and approachable by new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@dylwil3
Copy link

dylwil3 commented Nov 25, 2024

Bug description

# tmp.py
if len((x for x in [1, 2, 3])): # this code emits a TypeError
    print("yay!")

Command used

uvx pylint --disable=all --enable=C1802 tmp.py

Pylint output

************* Module tmp
tmp.py:1:3: C1802: Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty (use-implicit-booleaness-not-len)

------------------------------------------------------------------
Your code has been rated at 5.00/10 (previous run: 2.50/10, +2.50)

Expected behavior

No lint emitted. This should be a different error - len is not defined for generators in the first place. Notice, for example, that no lint is emitted for length used as condition with other standard Python objects that do not admit lengths (e.g. lambda expressions or functions).

Pylint version

pylint 3.3.1
astroid 3.3.5
Python 3.11.5 (main, Sep  8 2023, 16:31:47) [Clang 14.0.3 (clang-1403.0.22.14.1)]
@dylwil3 dylwil3 added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Nov 25, 2024
@zenlyj zenlyj added Good first issue Friendly and approachable by new contributors False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 26, 2024
@nedelcu-ioan
Copy link
Contributor

Hi, I’d like to work on this issue!

I believe the problem can be resolved by removing the commented line in the following code snippet:

# file: pylint/checkers/refactoring/implicit_booleaness_checker.py:128
        generator_or_comprehension = (
            nodes.ListComp,
            nodes.SetComp,
            nodes.DictComp,
            # nodes.GeneratorExp,
        )

Additionally, the test should be updated to:

# file: pylint/tests/functional/u/use/use_implicit_booleaness_not_len.py:128
    assert len(u + 1 for u in [])  # Should be fine

Let me know if this approach aligns with the expected resolution, and I’ll proceed to submit a PR.

@Pierre-Sassoulas
Copy link
Member

Thank you for proposing to work on this, I assigned you to the issue :)

Not sure if the proposed fix will work, but the new test looks fine (and we can also use the example given in the original issue).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Good first issue Friendly and approachable by new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

5 participants