Skip to content

Allow len-as-condition for explicit comparison #2684

@timokau

Description

@timokau

Steps to reproduce

if len([]) == 0:
  pass

pylint will lint this as len-as-condition. This seems to go back to PEP stating:

  • For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

Yes:

if not seq:`
if seq:

No:

if len(seq):
if not len(seq):

While I do agree that if len(seq) is not ideal, I don't think the same applies to if len(seq) == 0. I personally prefer the explicitness of the comparison and the applicability to everything that supports len. There is also the point of consistency in comarisons like:

if len(seq) == 0:
    pass
elif len(seq) == 1:
    pass

Current behavior

if len([]) and if len([]) == 0 are both linted.

Expected behavior

Only if len([]) is linted.

pylint --version output

pylint 2.2.2
astroid 2.1.0
Python 3.7.2 (default, Dec 24 2018, 03:41:55) 
[GCC 7.3.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement ✨Improvement to a componentGood first issueFriendly and approachable by new contributors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions