10000 `get_type_hints()` fails for types that are not subscriptable during runtime · Issue #819 · python/typing · GitHub
[go: up one dir, main page]

Skip to content

get_type_hints() fails for types that are not subscriptable during runtime #819

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

Closed
BvB93 opened this issue Jun 24, 2021 · 2 comments
Closed

Comments

@BvB93
Copy link
Contributor
BvB93 commented Jun 24, 2021

Somewhat related to #508.

The get_type_hints function currently assumes that if a stringified parametrized type is passed,
that it is subscriptable during runtime. This is assumption is by no means guaranteed to be true,
and while situations like these have become rarer since PEP 585 was implemented in Python 3.9,
there are still a number of cases inside and outside the standard library where this can go wrong
(e.g. functools.partial).

Examples

In [1]: from typing import Any
   ...: import numpy as np
   ...:
   ...: def func(dt: "np.dtype[Any]") -> None:
   ...:     pass
   ...:

In [2]: get_type_hints(func, {"np": np, "Any": Any})
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-7d6dbf62be59> in <module>
----> 1 get_type_hints(func, {"np": np, "Any": Any})

~\anaconda3\envs\numpy\lib\typing.py in get_type_hints(obj, globalns, localns, include_extras)
   1447         if isinstance(value, str):
   1448             value = ForwardRef(value)
-> 1449         value = _eval_type(value, globalns, localns)
   1450         if name in defaults and defaults[name] is None:
   1451             value = Optional[value]

~\anaconda3\envs\numpy\lib\typing.py in _eval_type(t, globalns, localns, recursive_guard)
    281     """
    282     if isinstance(t, ForwardRef):
--> 283         return t._evaluate(globalns, localns, recursive_guard)
    284     if isinstance(t, (_GenericAlias, GenericAlias)):
    285         ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)

~\anaconda3\envs\numpy\lib\typing.py in _evaluate(self, globalns, localns, recursive_guard)
    537                 localns = globalns
    538             type_ =_type_check(
--> 539                 eval(self.__forward_code__, globalns, localns),
    540                 "Forward references must evaluate to types.",
    541                 is_argument=self.__forward_is_argument__,

<string> in <module>

TypeError: 'numpy._DTypeMeta' object is not subscriptable
@gvanrossum
Copy link
Member

This is only one of many reasons why get_type_hints() sometimes raises even if a static checker considers an annotations valid. I'm not sure what you would like us to do about it? Why are you calling get_type_hints() (i.e., what's your use case)?

@Fidget-Spinner
Copy link
Member

@BvB93 that's on np.dtype to implement __class_getitem__, or numpy._DTypeMeta to implement __getitem__. Sorry but get_type_hints can't really do anything about that.

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

3 participants
0