You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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.
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.)
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?
The text was updated successfully, but these errors were encountered: