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: Fix false negatives in .py files when from __future__ import annotations is active #10362

Merged
merged 1 commit into from Mar 12, 2024

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Mar 12, 2024

Summary

Fixes #10340.

Currently, ruff emits no F821 rules on the following .py file, but the lines marked with XXX all fail at runtime due to undefined names, despite from __future__ import annotations being active:

from __future__ import annotations

from typing import TypeAlias, Optional, Union

MaybeCStr: TypeAlias = Optional[CStr]  # XXX
MaybeCStr2: TypeAlias = Optional["CStr"]  # always okay
CStr: TypeAlias = Union[C, str]  # XXX
CStr2: TypeAlias = Union["C", str]  # always okay

class C: ...

class Leaf: ...
class Tree(list[Tree | Leaf]): ...  # XXX
class Tree2(list["Tree | Leaf"]): ...  # always okay

This PR fixes that.

Much like #10341, it took me an annoying amount of time to figure out how to fix this, but it turned out to be hilariously easy to fix once I saw where to make the change 🙃

Test Plan

cargo test

@AlexWaygood AlexWaygood changed the title F821: Fix false negatives in .pyi files when from __future__ import annotations is active F821: Fix false negatives in .py files when from __future__ import annotations is active Mar 12, 2024
@AlexWaygood
Copy link
Member Author

AlexWaygood commented Mar 12, 2024

(Original title of the PR said ".pyi files", but I meant ".py files", the exact opposite -- sorry!)

@AlexWaygood AlexWaygood added bug Something isn't working linter Related to the linter labels Mar 12, 2024
Copy link

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@AlexWaygood AlexWaygood merged commit 704fefc into astral-sh:main Mar 12, 2024
17 checks passed
@AlexWaygood AlexWaygood deleted the f821-false-negs branch March 12, 2024 17:07
AlexWaygood added a commit that referenced this pull request Mar 21, 2024
…_ import annotations` is active (#10362)"

This reverts commit 704fefc.
AlexWaygood added a commit that referenced this pull request Mar 21, 2024
…_ import annotations` is active (#10362)"

This reverts commit 704fefc.
AlexWaygood added a commit that referenced this pull request Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working linter Related to the linter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

F821: false negatives when a file has from __future__ import annotations
2 participants