Repository navigation
typing._eval_type is not preserving GenericAlias subclasses #130870
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Mar 5, 2025 I'll make a pr on this branch
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Mar 7, 2025 - added a commit that references this issue
on May 12, 2025 @Viicos How does this cause a problem in practice? Changing
get_type_hintsin this way will end up exposing implementation details, and I'm not sure it's worth the hassle if all we're after is getting a nicerrepr().collections.abc.Callableis such an example because the generic alias used is aGenericAliassubclass, but this can happen with anyGenericAliassubclass (for instance with Pydantic, where we are trying to see how to use aGenericAliassubclass for parameterized generic Pydantic models). With this bug, it won't be possible to use generic Pydantic models in forward annotations.Changing
get_type_hintsin this way will end up exposing implementation detailsIf you are referring to
_should_unflatten_callable_args(), it is already there in the existing code. See my PR (the second one) for more details. I'll take a look at CI failures shortly.If you are referring to _should_unflatten_callable_args(), it is already there in the existing code.
I'm referring to the
_CallableGenericAliasinstance, which isn't available or documented anywhere else. My concern is that we'll encouraging people to do introspection on the private API, which isn't fun for us when we want to change things. (But maybe this isn't a huge issue for typing?)IMO, if you want this level of introspection, you should just use
__annotations__and parse things on your own. You're correctly opting in to extra maintenance by doing that.I'm referring to the
_CallableGenericAliasinstance, which isn't available or documented anywhere else.I just used this as an example to illustrate the bug.
_CallableGenericAliasis a private type, but subclassingGenericAliasis publicly documented and currently such aliases are lost during type evaluation intyping._eval_type()(relied on by the public functions such astyping.get_type_hints()). See also the added test in my PR.- It’s just an alternative approach…On Mon, Jun 9, 2025 at 07:15 Peter Bierma ***@***.***> wrote: *ZeroIntensity* left a comment (python/cpython#130870) <#130870 (comment)> Hm, ok, that makes more sense. For clarity: how is your PR (#131583 <#131583>) different from the first PR (#130897 <#130897>)? — Reply to this email directly, view it on GitHub <#130870 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/BNITW57VEOVPZPL5O2Y4QXD3CTRLJAVCNFSM6AAAAABYLSHBKWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNJUGQZTCNBVGQ> . You are receiving this because you commented.Message ID: ***@***.***>
- added a commit that references this issue
on Jul 5, 2025 No backport required, so the issue along with the initial fix (#130897) can be closed.
Bug report
Bug description:
In
typing._eval_type, generic aliases are reconstructed this way:cpython/Lib/typing.py
Lines 488 to 489 in e53d105
As
GenericAliasis subclassable, we can loose the actual subclass in some cases:I couldn't find a way to get actual bugs from it, but the repr is different:
The issue is also relevant for
typing._strip_annotations().Proposed fix
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
GenericAliassubclasses intyping.get_type_hints()#131583