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
typing.get_type_hints()
works for Callable[[], None]
from the typing
module, but not collections.abc
#91621
Comments
Reproduced, and I agree that this looks like a regression in 3.11. On 3.10: C:\Users\alexw\coding>python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing, collections.abc
>>> def f(x: collections.abc.Callable[[], None]): ...
...
>>> typing.get_type_hints(f)
{'x': collections.abc.Callable[[], None]} On 3.11: C:\Users\alexw\coding>python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing, collections.abc
>>> def f(x: collections.abc.Callable[[], None]): ...
...
>>> typing.get_type_hints(f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 2277, in get_type_hints
hints[name] = _eval_type(value, globalns, localns)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 353, in _eval_type
t = t.__origin__[args]
~~~~~~~~~~~~^^^^^^
File "<frozen _collections_abc>", line 430, in __new__
TypeError: Callable must be used as Callable[[arg, ...], result]. |
Looks like this is a result of #30900 cc @NiklasRosenstein @JelleZijlstra |
The complexity here traces back to some of the decisions made in https://bugs.python.org/issue42195 We do this kind of thing a couple times in typing.py, I can open a PR:
|
|
This mirrors logic in typing.get_args. The trickiness comes from how we flatten args in collections.abc.Callable, see https://bugs.python.org/issue42195
) This mirrors logic in typing.get_args. The trickiness comes from how we flatten args in collections.abc.Callable, see https://bugs.python.org/issue42195
Thanks @Zac-HD for the pre-beta testing and @hauntsaninja for the patch! |
Zac-HD commentedApr 16, 2022
•
edited
Bug report
typing.get_type_hints()
works forCallable[[], None]
from thetyping
module, but notcollections.abc
. This is the standard way to annotate a callable which takes no arguments, and the generic types fromtyping
should behave identically to those incollections.abc
.Your environment
CPython 3.11.0a7 (and I believe back to a3), found via CI for Hypothesis, and reproduced in Ubuntu/WSL2 locally.
I'm skipping that test for now, and will re-enable when this is fixed.
The text was updated successfully, but these errors were encountered: