pylint
pylint copied to clipboard
Expand to ``use-implicit-booleaness-not-len`` to catch `len(iterable) == 0` and `>0`
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.
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 first timer here, could I pick up this issue?
Sure, I assigned you to it :)
Hi, could i be assigned to this please?
@chethanagopinath do you still want to work on this issue ?
@Pierre-Sassoulas , seems like @chethanagopinath is awol. Maybe re-assign to @joonhoswe ?
@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.
Hi, could I work on this issue?
Sure, I assigned you to it, let us know if it's a problem @joonhoswe
hey @Pierre-Sassoulas can i work on this?
@mchris86 how far along are you, are you still working on it ?
@Pierre-Sassoulas is it okay if i issue a PR on this?
Yes go on thank you for working on this :)
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?
@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!
Let's discuss exclusively in the pull request please.