Skip to content

PLR1704 triggers for list comprehension variable #12339

@richardebeling

Description

@richardebeling

This code snippet

def func(foo):
    for _ in bar(foo for foo in [1]):
        pass

triggers PLR1704 with ruff 0.5.2

$ ruff --version
ruff 0.5.2

$ ruff check --select PLR1704 test.py
test.py:2:26: PLR1704 Redefining argument with the local name `foo`
  |
1 | def func(foo):
2 |     for _ in bar(foo for foo in [1]):
  |                          ^^^ PLR1704
3 |         pass
  |

Found 1 error.

which is wrong. From the python3 docs:

However, aside from the iterable expression in the leftmost for clause, the comprehension is executed in a separate implicitly nested scope. This ensures that names assigned to in the target list don’t “leak” into the enclosing scope.

Metadata

Metadata

Assignees

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