Repository navigation
typing.get_type_hints() works for Callable[[], None] from the typing module, but not collections.abc #91621
Copy link
Copy link
Closed
Labels
3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Apr 16, 2022 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory3.11only security fixesonly security fixes
on Apr 16, 2022 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:
diff --git a/Lib/typing.py b/Lib/typing.py index 3e0fbdb989..af424c2109 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -350,7 +350,11 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()): ForwardRef(arg) if isinstance(arg, str) else arg for arg in t.__args__ ) - t = t.__origin__[args] + if (t.__origin__ is collections.abc.Callable + and not (len(args) == 2 and _is_param_expr(args[0]))): + t = t.__origin__[(args[:-1], args[-1])] + else: + t = t.__origin__[args] ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__) if ev_args == t.__args__: return tReacted by Jelle ZijlstraThe 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:
🤦 . Seeing this again, I'm sad that the special case has to exist. Unfortunately, this seems like an easy way to trip up new contributors. Please open a PR.
Thanks @Zac-HD for the pre-beta testing and @hauntsaninja for the patch!
Reacted by Alex Waygood
Metadata
Metadata
Assignees
Labels
3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Bug report
typing.get_type_hints()works forCallable[[], None]from thetypingmodule, but notcollections.abc. This is the standard way to annotate a callable which takes no arguments, and the generic types fromtypingshould 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.