-
Notifications
You must be signed in to change notification settings - Fork 25
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
PEP 585 support in 3.7+ using __future__ #66
Comments
I also discovered this earlier but didn't have time to organize it and create an issue :) Just want to point out some important points to consider for implementation:
from __future__ import annotations
def foo() -> list[int]:
return [0]
foo() is 3.7+, but this one: from __future__ import annotations
import typing
def foo() -> list[int]:
return [0]
typing.get_type_hints(foo) is 3.9+. Probably there could be a command line option for users to inform
def foo():
x: list[int] is 3.6+ (since variable annotations are 3.6+), even if
from __future__ import annotations
x: list[int] # PEP 585, 3.9+
y: int | str # PEP 604, 3.10+ is 3.7+, since from __future__ import annotations
def foo():
x: (a := 1) is still 3.8+.
will still be 3.10+ since the PEP 563 is always enabled (without having to write |
Thanks for the feedback, @TylerYep and @gousaiyang! I will try to look into this soon. |
Just want to add one more case: When the target being annotated is not a simple name (e.g. |
@gousaiyang I've added commit e231317 to branch (I will have a look at 3.10 stuff later btw) |
This is a good start which allows users to choose whether to evaluate annotations or not, and I think this will work for most cases. However, if better accuracy is desired, the implementation should still consider the corner cases mentioned above. Unfortunately I could tell that will require much more work than your current Example: The following code: from __future__ import annotations
import typing
x: typing.Final[int] = 1 is actually 3.7+. Evaluation of import typing
x: 'typing.Final[int]' = 1 (This is 3.6+) Your current implementation only suppressed builtin generic type annotations ( Also if a file does not have In addition, Python annotations really have a lot of corner cases, and my previous claim about annotating complex targets (not a simple name) is inaccurate. It turns out that they are evaluated even when PEP 563 is enabled, and people are proposing to suppress this in 3.10. There's even more to mention about the evaluation of targets themselves. In It's also good to skip evaluating code under Implementing all of those will be a ton of work. But I think if the AST-level exclusion (ignore annotation nodes) is added to your current implementation, it should be useful enough for many people. |
Thanks for the great notes, @gousaiyang. Yeah that will be a lot of work and some of it might need to happen sooner or later, but right now I agree with your assessments. Annotations are kind of a pain.. Haha. Added e32f92c. It's ignoring the nodes.
What exactly do you mean here? |
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Final
x: 'Final[int]' = 1 By using |
Thanks for the feedback. Pushed 99240ed with corrections. I'm inclined not to add special code for |
I think that looks good.
It's OK since users can always use |
Ignore annotation AST nodes if not evaluating annotations
Thanks again, both. Will release version 1.1.1 soon. |
And use postponed evaluation of annotations [2] to avoid syntax errors on Python 3.7 and 3.8. See [3] for why it works. [1] https://peps.python.org/pep-0585/ [2] https://peps.python.org/pep-0563/ [3] netromdk/vermin#66 (comment)
Describe the bug
Vermin reports the minimum version is Python 3.9, even though the code will run correctly in Python 3.7+.
To Reproduce
Steps to reproduce the behavior.
Run the following code in Python 3.7, 3.8, 3.9
Output:
Expected behavior
Minimum version should be Python 3.7
Environment:
The text was updated successfully, but these errors were encountered: