Skip to content

flake8-comprehensions (C4) - Remove unnecessary list comprehensions in additional contexts that can take arbitrary iterables (C419) #12648

@gandhis1

Description

@gandhis1

The following code all creates unnecessary list comprehensions

    a = all([i is not None for i in range(10)])
    b = tuple([i * 2 for i in range(10)])
    c = sum([i * 2 for i in range(10)])
    d = ",".join([str(i * 2) for i in range(10)])

ruff will only suggest changes on the first line:

stuff.py:3:13: C419 Unnecessary list comprehension
  |
2 | def main() -> None:
3 |     a = all([i is not None for i in range(10)])
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C419
4 |     b = tuple([i * 2 for i in range(10)])
5 |     c = sum([i * 2 for i in range(10)])
  |
  = help: Remove unnecessary list comprehension

However all of these are unnecessary list comprehensions. It ought to be able to identify them all. For one, the docs suggest that sum is covered, but from the above, it appears to not fully be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomershelp wantedContributions especially welcomeruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions