Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
Bug description:
Intyping._eval_type, generic aliases are reconstructed this way:
Lines 488 to 489 ine53d105
| ifisinstance(t,GenericAlias): | |
| returnGenericAlias(t.__origin__,ev_args) |
AsGenericAlias is subclassable, we can loose the actual subclass in some cases:
fromtypingimportget_type_hintsfromcollections.abcimportCallableC=Callable[[str,'int'],int]C.__class__#> <class 'collections.abc._CallableGenericAlias'>C.__class__.__bases__#> (<class 'types.GenericAlias'>,)classA:c:Chints=get_type_hints(A)hints['c'].__class__#> <class 'types.GenericAlias'>
I couldn't find a way to get actual bugs from it, but the repr is different:
hints['c']#> collections.abc.Callable[str, int, int]C#> collections.abc.Callable[[str, 'int'], int]
The issue is also relevant fortyping._strip_annotations().
Proposed fix
diff --git a/Lib/typing.py b/Lib/typing.pyindex 4b3c63b25ae..25e0576839f 100644--- a/Lib/typing.py+++ b/Lib/typing.py@@ -486,7 +486,9 @@ def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=f if ev_args == t.__args__: return t if isinstance(t, GenericAlias):- return GenericAlias(t.__origin__, ev_args)+ if _should_unflatten_callable_args(t, ev_args):+ return t.__class__(t.__origin__, (ev_args[:-1], ev_args[-1]))+ return t.__class__(t.__origin__, ev_args) if isinstance(t, Union): return functools.reduce(operator.or_, ev_args) else:
CPython versions tested on:
3.13
Operating systems tested on:
Linux