Skip to content

SIM222 false positive in [*iterable] or [another list] #14237

@rdaysky

Description

@rdaysky

Suppose I want to run some code for each element in an iterable, but if there are none, run once for the value of None. However, the natural code

def f(some_iterable):
    for x in [*some_iterable] or [None]:
        do_something(x)

fails SIM222, which wants to remove the or [None] part, presumably thinking the list will always be truthy. Similarly to #9479, the rule needs to take into account that a list/set/dict literal containing only *-elements can still end up empty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions