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

F821 Undefined name item when assigning a set comprehension in an if-clause #9230

Closed
kurellajunior opened this issue Dec 21, 2023 · 4 comments · Fixed by #9248
Closed

F821 Undefined name item when assigning a set comprehension in an if-clause #9230

kurellajunior opened this issue Dec 21, 2023 · 4 comments · Fixed by #9248
Assignees
Labels
bug Something isn't working

Comments

@kurellajunior
Copy link

kurellajunior commented Dec 21, 2023

import logging
from itertools import groupby


class MonocausalTransformer:
    for key, tuples in groupby([("a",1),("a", 2),("b",3)], lambda t: t[0]):
        if len(replacements := {item[1] for item in tuples}) > 1:
            logging.error("Multiple replacements for %s: %s", key, replacements)

When running ruff . --fix on the whole project it gives this error message: F821 Undefined name `item`
But it works just as expected when executed
And extracting the set to a separate variable a line above works also fine

$ ruff --version
ruff 0.1.7
$ ruff --fix item.py 
item.py:7:33: F821 Undefined name `item`
Found 1 error.
[tool.ruff]
target-version = "py311"
line-length = 120
select = ["ALL"]
ignore = [
    "A003",
    "ARG002",  # unused method arguments, e.g. when implementing an interface
    "D10",  # TODO: Add doc messages to public api, or make private
    "D203",  # Conflicting with D211
    "D213",  # Conflicting with D212
    "TD002", "TD003", "FIX002",  # TODO: Fix all todo comments
    "PLC1901",  # We have to distinguish between None and ""
    "RUF012",  # Ignored already for pydantic models, but we use our own base class
    "S311",  # No cryptography happening here
]

https://play.ruff.rs/27d1104c-b86e-45dc-98c6-fc6eac195bcb

@charliermarsh charliermarsh added the bug Something isn't working label Dec 21, 2023
@charliermarsh
Copy link
Member

Thanks! Does look like a bug.

@charliermarsh
Copy link
Member

Is this the complete snippet? I'm unable to reproduce in the playground: https://play.ruff.rs/dd91b36b-c177-4e84-b190-368de569f6a4

@charliermarsh charliermarsh added the question Asking for support or clarification label Dec 21, 2023
@kurellajunior
Copy link
Author

kurellajunior commented Dec 21, 2023

Is this the complete snippet? I'm unable to reproduce in the playground: play.ruff.rs/dd91b36b-c177-4e84-b190-368de569f6a4

You are right, I extracted only a small snippet, thought it was enough. I extracted now a larger snippet, stripped it down to the actual minimum: https://play.ruff.rs/27d1104c-b86e-45dc-98c6-fc6eac195bcb
This now gives on my console, in my poetry environment with ruff 0.1.7 the documented error:

$ ruff --fix item.py 
item.py:26:33: F821 Undefined name `item`
Found 1 error.

As soon as I remove the class context, the error is gone

Also, when I put everything inside __init__() the error is gone

Also updated the original post

@charliermarsh
Copy link
Member

Thanks! Assume this has to do with accessing from within a class scope. We will fix.

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
Development

Successfully merging a pull request may close this issue.

2 participants