Skip to content

Make UP028 always fixable again #16496

@InSyncWithFoo

Description

@InSyncWithFoo

After #16451, UP028 is no longer always fixable. However, a different kind of fix can be offered in such cases:

for some_global, some_nonlocal in iterable:
    yield some_global, some_nonlocal
for _element in iterable:
    some_global, some_nonlocal = _element
    yield some_global, some_nonlocal

I can't think of any edge cases, other than that adding a new name to the scope might break runtime inspections:

nonlocal some_nonlocal

for some_nonlocal in iterable:
    yield some_nonlocal

print(locals())  # {'some_nonlocal': ...}
nonlocal some_nonlocal

for _element in iterable:
    some_nonlocal = _element
    yield some_nonlocal

print(locals())  # {'some_nonlocal': ..., '_element': ...}

Also consider the case where the new name is the same as an existing one:

_element = ...

for _element in iterable:
    some_global, some_nonlocal = _element
    yield some_global, some_nonlocal

print(_element)  # !!!

Generating a new, unique variable name is trivial, but it does, however, necessitate the fix being marked as unsafe to give the user a chance to review that name (along with everything else).

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixesRelated to suggested fixes for violationswishNot on the current roadmap; maybe in the future

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions