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 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]: fromtypingimportAny
...: importnumpyasnp
...:
...: deffunc(dt: "np.dtype[Any]") ->None:
...: pass
...:
In [2]: get_type_hints(func, {"np": np, "Any": Any})
---------------------------------------------------------------------------TypeErrorTraceback (mostrecentcalllast)
<ipython-input-2-7d6dbf62be59>in<module>---->1get_type_hints(func, {"np": np, "Any": Any})
~\anaconda3\envs\numpy\lib\typing.pyinget_type_hints(obj, globalns, localns, include_extras)
1447ifisinstance(value, str):
1448value=ForwardRef(value)
->1449value=_eval_type(value, globalns, localns)
1450ifnameindefaultsanddefaults[name] isNone:
1451value=Optional[value]
~\anaconda3\envs\numpy\lib\typing.pyin_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
The text was updated successfully, but these errors were encountered:
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)?
@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.
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
The text was updated successfully, but these errors were encountered: