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

[Enhancement]: len(...) > 0 -> len(...) #327

Closed
opk12 opened this issue Feb 8, 2024 · 1 comment
Closed

[Enhancement]: len(...) > 0 -> len(...) #327

opk12 opened this issue Feb 8, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@opk12
Copy link

opk12 commented Feb 8, 2024

Overview

What about a diagnostic to change

if len(...) > 0:
    ...

into

if len(...):
    ...

Proposal

I think they can be considered equivalent, because

>>> class C:
...     def __len__(self):
...         return -1
... 
>>> len(C())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: __len__() should return >= 0

pylint --enable-all-extensions a.py does not warn about it.

I am a new user and do not actually know if this is in-scope for the project, sorry if it isn't the case.

@opk12 opk12 added the enhancement New feature or request label Feb 8, 2024
@dosisod
Copy link
Owner

dosisod commented Feb 9, 2024

This is already covered by FURB115:

nums = []

if len(nums) > 0:
    pass

Running Refurb:

$ refurb x.py
x.py:3:4 [FURB115]: Replace `len(x) > 0` with `x`

Since empty containers are falsey, len(x) can be further simplified to just x.

@dosisod dosisod closed this as completed Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants