-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Avoid flagging __future__
annotations as required for non-evaluated type annotations
#11414
Conversation
__future__
annotations as required for non-evaluated type annotations
Hmm, I wouldn't do this for quoted annotations. I think it was definitely intentional in the original flake8 plugin for PEP-585 annotations in quotes to trigger this check, because otherwise UP037 won't automatically remove the quotes for you without the |
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
FA102 | 3 | 0 | 3 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+0 -3 violations, +0 -0 fixes in 1 projects; 43 projects unchanged)
zulip/zulip (+0 -3 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL
- corporate/lib/stripe.py:3017:29: FA102 Missing `from __future__ import annotations`, but uses PEP 604 union - corporate/lib/stripe.py:3115:48: FA102 Missing `from __future__ import annotations`, but uses PEP 604 union - corporate/lib/stripe.py:915:40: FA102 Missing `from __future__ import annotations`, but uses PEP 604 union
Changes by rule (1 rules affected)
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
FA102 | 3 | 0 | 3 | 0 | 0 |
The diagnostic and documentation aren't really correct or consistent with that use-case though, at least as-written. The messaging suggests you need this to avoid runtime errors. Almost feels like it needs an explicit message (e.g., you could remove quotes by adding this), but in that case, isn't it always true that adding |
I'm now wondering if that should be a separate rule or part of FA100? Since the type can be written more succinctly if you import |
Huh, that's not how I've ever interpreted that message. The message is "Missing The original flake8 plugin explicitly mentions pyupgrade in its README as one of the motivations for the plugin: https://github.com/TylerYep/flake8-future-annotations?tab=readme-ov-file#flake8-future-annotations. And I don't think this rule is nearly adequate to prevent all runtime errors arising from using newer typing syntax on older versions, nor should it try to do that.
Not necessarily... eg. removing quotes from this annotation would cause a syntax error: def foo() -> "Some kind of string":
pass |
IDK, because I've always thought of "enabling other tools to auto-upgrade your annotations" as being the primary motivation of the rule 😄 |
Are you mixing up FA100 and FA102? Or am I mixing them up? FA100 is the rule that tells you "you can write this more succinctly if you import |
This rule modifies FA102, which tells you that you're using an unsupported type annotation that will work if you add |
Argh, I should have checked more thoroughly. Yikes, and sorry :( Yes, I didn't realise there was a distinction between It would be good if the rule summary at https://docs.astral.sh/ruff/rules/#flake8-future-annotations-fa made the distinction between the rules here a bit clearer. |
No need to apologize, I was also unsure. My guess is that we don't emit |
LGTM! Thank you for such swift reply |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense.
947a5bb
to
a3d8b53
Compare
Confirmed that we still emit |
TYVM! |
## Summary Similar to #11414, this PR extends `UP037` to flag quoted annotations that are located in positions that won't be evaluated at runtime. For example, the quotes on `Tuple` are unnecessary in: ```python from typing import TYPE_CHECKING if TYPE_CHECKING: from typing import Tuple def foo(): x: "Tuple[int, int]" = (0, 0) foo() ```
@charliermarsh This is only accurate in Python versions 3.10+ I believe:
|
But quoted ones will work regardless of the Python version:
I didn't read the PR thoroughly, but wanted to make sure the lint rule is only changed for the quoted version but still flags non-quoted lowercase types without future annotations? |
@TylerYep - the logic here only applies to annotated assignments within function bodies, which I believe works on all Python versions: def f():
x: list[int] = 1
f() |
Ah, makes sense, thank you for clarifying! |
Summary
If an annotation won't be evaluated at runtime, we don't need to flag
from __future__ import annotations
as required. This applies both to quoted annotations and annotations outside of runtime-evaluated positions, like:Closes #11397.