question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PEP 585 support in 3.7+ using __future__

See original GitHub issue

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

from __future__ import annotations

def fn(x: list[int]) -> list[int]:
    a: list[int] = []
    return a

fn([])

Output:

Detecting python files..
Analyzing using 12 processes..
!2, 3.9      /Users/tyler.yep/Documents/Github/probs/yo.py
  L1 C5: '__future__' module requires 2.1, 3.0
  L1 C5: '__future__.annotations' member requires !2, 3.7
  L4: annotations requires !2, 3.0
  L4: builtin generic type annotation (list[..]) requires !2, 3.9
  L5: variable annotations requires !2, 3.6
  L5: builtin generic type annotation (list[..]) requires !2, 3.9
  L6: builtin generic type annotation (list[..]) requires !2, 3.9

Minimum required versions: 3.9
Incompatible versions:     2

Expected behavior Minimum version should be Python 3.7

Environment:

  • Vermin latest version (1.1.0)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
gousaiyangcommented, Mar 6, 2021

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:

  • PEP 563 (from __future__ import annotations) makes function and variable annotations stored in string form. They are not evaluated at definition time, but only evaluated when you manually call typing.get_type_hints or eval(obj.__annotations__). The following code snippet:
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 vermin whether the annotations in the code will be manually evaluated or not.

  • Local variable annotations are never evaluated or stored anywhere. So this:
def foo():
    x: list[int]

is 3.6+ (since variable annotations are 3.6+), even if from __future__ import annotations is not present.

  • Even if PEP 563 is enabled, annotations should still be syntactically valid Python expressions. New syntax used should still indicate a higher minimum version. For example,
from __future__ import annotations
x: list[int]  # PEP 585, 3.9+
y: int | str  # PEP 604, 3.10+

is 3.7+, since list[int] and int | str are valid syntax in Python 3.7. While this one:

from __future__ import annotations
def foo():
    x: (a := 1)

is still 3.8+.

  • PEP 563 is mandatory in Python 3.10. Let’s assume that int | str were a 3.11 feature (instead of 3.10 in reality). The following code
x: int | str

will still be 3.10+ since the PEP 563 is always enabled (without having to write from __future__ import annotations) in 3.10 and int | str is not new syntax.

1reaction
netromdkcommented, Apr 18, 2021

Thanks again, both. Will release version 1.1.1 soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is PEP 585 unusable at runtime under Python 3.7 and 3.8?
For use cases restricted to type annotations, Python files with the annotations future-import (available since Python 3.7) can parameterize ...
Read more >
PEP 585 – Type Hinting Generics In Standard Collections
This PEP proposes to enable support for the generics syntax in all ... Starting with Python 3.7, when from __future__ import annotations is ......
Read more >
PEP 585 parameterized built-in classes are not recognized ...
Starting with Python 3.7, when from future import annotations is used, function and variable annotations can parameterize standard collections directly. Example ...
Read more >
Annotation issues at runtime - mypy 0.991 documentation
Use of from __future__ import annotations (PEP 563) (this behaviour may ... There is limited support for using this syntax in Python 3.7...
Read more >
Python Type Hints - How to Upgrade Syntax with pyupgrade
PEP 585 added generic syntax to builtin types. ... But we can use both syntaxes today, even from Python 3.7, with Mypy 0.800+...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found