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

Runtime access to type annotations #530

Closed
ghost opened this issue Dec 13, 2014 · 2 comments
Closed

Runtime access to type annotations #530

ghost opened this issue Dec 13, 2014 · 2 comments

Comments

@ghost
Copy link

ghost commented Dec 13, 2014

The current implementation erases type information from annotations and friends at runtime:

In [2]: def f(x: List[int]) -> int:
...: pass

In [3]: f.annotations
Out[3]: {'return': int, 'x': list} ## lost x: List[int]

Is this by design, and if so, why? Is there some reason to not keep an accurate run-time representation of type annotations? Could that information be made available uniformly on every typeable thing?

In [4]: class A:
...: i = Undefined(int)

In [5]: A.annotations ## lost i: int

Perhaps even include a run-time representation of some inferred types e.g. all types of all instance attributes might be made available via class.annotations (or something like it), regardless of how those types were declared (on init or via class attributes), or even inferred (by mypy from body of init or otherwise).

Thoughts?

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 17, 2015

Mypy will eventually keep track of type arguments of types such as List[int] (in function annotations), as this is part of the current PEP 484 draft (https://github.com/ambv/typehinting). Keeping track of attribute annotations is trickier, since comments can also be used for them and those will be lost anyway at runtime. Also, most variable types can be inferred and inferred types aren't available at runtime.

@gvanrossum
Copy link
Member

The prototype of a new typing.py that I'm developing in https://github.com/ambv/typehinting/tree/master/prototyping has this feature. E.g. Tuple[int].__tuple_params__ == (int,). (I don't have List and abc.collections yet, but I do have Generic[T] etc.)

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

No branches or pull requests

2 participants