google / pytype

A static type analyzer for Python code

Home Page:https://google.github.io/pytype

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

__future__.annotations breaks local imports

eltoder opened this issue · comments

When using from __future__ import annotations, locally imported names cannot be used in type annotations. Example:

from __future__ import annotations

def test():
    from datetime import datetime

    def fn1(x: float) -> datetime:
        return datetime.fromtimestamp(x)

    def fn2(x: datetime) -> float:
        return x.timestamp()

This is clearly OK and works without the future import. With the future import it produces:

File "/home/elt/code/pytype-test/proj/local_import.py", line 6, in test: Name 'datetime' is not defined [name-error]
File "/home/elt/code/pytype-test/proj/local_import.py", line 9, in test: Name 'datetime' is not defined [name-error]

going to close this as infeasible - future annotations are effectively deprecated (they will never become the default behaviour in python, and i would be unsurprised if they are removed from __future__ at some point). furthermore local imports are a poorly-supported feature at best (and low-priority for pytype) so this is simply an unfortunate confluence of features.

this pydantic thread outlines some of the issues with future annotations; it doesn't apply identically to pytype's situation but it is a good illustration of the ways in which they are problematic.