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

Incorrect F821 raised for list comprehension in Annotated #7879

Closed
Harry-Lees opened this issue Oct 9, 2023 · 3 comments · Fixed by #7885
Closed

Incorrect F821 raised for list comprehension in Annotated #7879

Harry-Lees opened this issue Oct 9, 2023 · 3 comments · Fixed by #7885
Assignees
Labels
bug Something isn't working

Comments

@Harry-Lees
Copy link

Harry-Lees commented Oct 9, 2023

Using Ruff 0.0.292 on the following code snippet

from typing import Annotated

foo = [1, 2, 3, 4, 5]

class Bar:
    baz: Annotated[
        str,
        [qux for qux in foo],
    ]

The following error is raised:

test.py:8:10: F821 Undefined name `qux`
Found 1 error.

The minimal reproducable example looks a little bit weird, but I came across this when writing a pydantic validator on some (IMO) reasonable pydantic code. A more sensible example can be seen below.

from typing import Annotated

from pydantic import BaseModel
from pydantic.functional_validators import BeforeValidator

foo = [1, 2, 3, 4, 5]

class Bar(BaseModel):
    baz: Annotated[
        list[int], BeforeValidator(lambda values: [value**2 for value in values])
    ]

qux = Bar(baz=foo)
test.py:10:52: F821 Undefined name `value`
Found 1 error.
@charliermarsh
Copy link
Member

Thanks!

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

The cause here isn't immediately obvious to me, but definitely looks like a bug.

@charliermarsh charliermarsh self-assigned this Oct 10, 2023
@charliermarsh
Copy link
Member

I see the error here.

charliermarsh added a commit that referenced this issue Oct 10, 2023
## Summary

Given:

```python
baz: Annotated[
    str,
    [qux for qux in foo],
]
```

We treat `baz` as `BindingKind::Annotation`, to ensure that references
to `baz` are marked as unbound. However, we were _also_ treating `qux`
as `BindingKind::Annotation`, which meant that the load in the
comprehension _also_ errored.

Closes #7879.
konstin pushed a commit that referenced this issue Oct 11, 2023
## Summary

Given:

```python
baz: Annotated[
    str,
    [qux for qux in foo],
]
```

We treat `baz` as `BindingKind::Annotation`, to ensure that references
to `baz` are marked as unbound. However, we were _also_ treating `qux`
as `BindingKind::Annotation`, which meant that the load in the
comprehension _also_ errored.

Closes #7879.
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