Skip to content
New issue

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

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

Already on GitHub? # to your account

PLR1704 triggers for list comprehension variable #12339

Closed
richardebeling opened this issue Jul 15, 2024 · 1 comment · Fixed by #12346
Closed

PLR1704 triggers for list comprehension variable #12339

richardebeling opened this issue Jul 15, 2024 · 1 comment · Fixed by #12346
Assignees
Labels
bug Something isn't working

Comments

@richardebeling
Copy link

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.

@charliermarsh charliermarsh added the bug Something isn't working label Jul 16, 2024
@charliermarsh
Copy link
Member

Agreed -- that looks like a bug.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants