Skip to content

Unnecessary list() or comprehension in Counter() #21588

@injust

Description

@injust

Summary

Counter (as a dict subclass) should have a rule similar to https://docs.astral.sh/ruff/rules/unnecessary-comprehension-in-call/ and https://docs.astral.sh/ruff/rules/unnecessary-list-comprehension-dict/:

Counter([(x, f(x)) for x in foo])
Counter([x.id for x in foo])

# above lines should be rewritten to:
Counter((x, f(x)) for x in foo)
Counter(x.id for x in foo)

As well as https://docs.astral.sh/ruff/rules/unnecessary-comprehension/:

Counter([x for x in foo])
# above line is rewritten to:
Counter(list(foo))
# but it should really be rewritten to:
Counter(foo)
# this should be rewritten too:
Counter(x for x in foo)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions