pylint icon indicating copy to clipboard operation
pylint copied to clipboard

Expand to ``use-implicit-booleaness-not-len`` to catch `len(iterable) == 0` and `>0`

Open DaniBodor opened this issue 8 months ago • 7 comments

Current problem

C1802 currently catches this

Examples 1&2 (currently caught by rule C1802)

fruits = ["orange", "apple"]
vegetables = []

if len(fruits):
    print(fruits)

if not len(vegetables):
    print(vegetables)

I come across the following sometimes as well (potentially in botched attempt to fix the the flagged violation above), which I think should be equally flagged as a violation.

Examples 3&4 (not caught by PLC1802)

fruits = ["orange", "apple"]
vegetables = []

if len(fruits) > 0:
    print(fruits)

if len(vegetables) == 0:
    print(vegetables)

Examples 5&6 (recommended formulation)

fruits = ["orange", "apple"]
vegetables = []

if fruits:
    print(fruits)

if not vegetables:
    print(vegetables)

Desired solution

Examples 3&4 to be flagged as well

Additional context

See our previous discussion on this issue.

DaniBodor avatar Mar 17 '25 16:03 DaniBodor

Thank you for opening the issue, it make sense (especially considering that trying to fix the issue without understanding that empty iterator are falsey often result in code like this)

Pierre-Sassoulas avatar Mar 17 '25 21:03 Pierre-Sassoulas

@Pierre-Sassoulas first timer here, could I pick up this issue?

chethanagopinath avatar Mar 20 '25 02:03 chethanagopinath

Sure, I assigned you to it :)

Pierre-Sassoulas avatar Mar 20 '25 06:03 Pierre-Sassoulas

Hi, could i be assigned to this please?

joonhoswe avatar May 01 '25 17:05 joonhoswe

@chethanagopinath do you still want to work on this issue ?

Pierre-Sassoulas avatar May 01 '25 19:05 Pierre-Sassoulas

@Pierre-Sassoulas , seems like @chethanagopinath is awol. Maybe re-assign to @joonhoswe ?

DaniBodor avatar Jun 03 '25 22:06 DaniBodor

@Pierre-Sassoulas apologies for the late reply and delay here, my notifications did not catch the previous tag. Thanks for checking, I had made some progress on a branch locally but couldn't take it all the way, feel free to reassign.

chethanagopinath avatar Jun 04 '25 00:06 chethanagopinath

Hi, could I work on this issue?

mchris86 avatar Sep 24 '25 13:09 mchris86

Sure, I assigned you to it, let us know if it's a problem @joonhoswe

Pierre-Sassoulas avatar Sep 24 '25 16:09 Pierre-Sassoulas

hey @Pierre-Sassoulas can i work on this?

Kaos599 avatar Oct 12 '25 18:10 Kaos599

@mchris86 how far along are you, are you still working on it ?

Pierre-Sassoulas avatar Oct 12 '25 18:10 Pierre-Sassoulas

@Pierre-Sassoulas is it okay if i issue a PR on this?

Kaos599 avatar Oct 13 '25 17:10 Kaos599

Yes go on thank you for working on this :)

Pierre-Sassoulas avatar Oct 13 '25 17:10 Pierre-Sassoulas

Hey @Pierre-Sassoulas , I've submitted PR #10658 extending C1802 to detect len() comparisons with zero (e.g., len(x) == 0, len(x) > 0). The implementation adds detection for 6 additional comparison patterns.

can you please review the changes and let me know the feedback, Thanks!


If this PR is okay to be merged could you kindly please assign this with hacktoberfest label as well?

Kaos599 avatar Oct 13 '25 18:10 Kaos599

@Pierre-Sassoulas

The C1802 checker was missing len() comparisons with zero that could be simplified using implicit booleaness (e.g., len(seq) == 0 → not seq, len(seq) > 0 → seq). I Extended it to catch these patterns while fixing a false positive where compound expressions like return a and len(x) > 0 were incorrectly flagged. Let me know if you'd like any adjustments!

Kaos599 avatar Oct 14 '25 06:10 Kaos599

Let's discuss exclusively in the pull request please.

Pierre-Sassoulas avatar Oct 14 '25 06:10 Pierre-Sassoulas