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

__future__.annotations breaks local imports #1453

Closed
eltoder opened this issue Jun 29, 2023 · 1 comment
Closed

__future__.annotations breaks local imports #1453

eltoder opened this issue Jun 29, 2023 · 1 comment

Comments

@eltoder
Copy link

eltoder commented Jun 29, 2023

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]
@martindemello
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants