dosisod/refurb

[Enhancement]: `len(...) > 0` -> `len(...)`

opk12 opened this issue · 1 comments

opk12 commented

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.

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.