Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rdaysky opened this issue Nov 10, 2024 · 1 comment · Fixed by #14263
Closed

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

rdaysky opened this issue Nov 10, 2024 · 1 comment · Fixed by #14263
Labels
bug Something isn't working

Comments

@rdaysky
Copy link

rdaysky commented Nov 10, 2024

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.

@rdaysky rdaysky changed the title SIM222 false positive in if [*iterable] or [another list] SIM222 false positive in [*iterable] or [another list] Nov 10, 2024
@InSyncWithFoo
Copy link
Contributor

InSyncWithFoo commented Nov 10, 2024

The same holds for other literal iterables as well (playground):

[*a] or ...
[*a, *b] or ...
{*a} or ...
{*a, *b} or ...
(*a,) or ...
(*a, *b) or ...
{**a} or ...
{**a, **b} or ...

I'll work on a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants