https://github.com/python/cpython/commit/de2ea9aaefebd1b06e5302c1858d70e00cd63639
commit: de2ea9aaefebd1b06e5302c1858d70e00cd63639
branch: main
author: Victorien <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-08-29T02:56:34Z
summary:
Do not recurse into `Literal` arguments during type evaluation (#156534)
Unlike other generic aliases, `Literal` arguments aren't type expressions
(e.g. `Literal[1, 'a'].__args__ == (1, 'a')`. As such, there is no need
to recurse into all the arguments as they are guaranteed to be returned
unchanged.
files:
M Lib/typing.py
diff --git a/Lib/typing.py b/Lib/typing.py
index 65e1d1ea6be584..99c467a8af07d8 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -475,6 +475,9 @@ def _eval_type(t, globalns, localns, type_params, *,
recursive_guard=frozenset()
type_params=type_params, owner=owner,
_recursive_guard=recursive_guard,
format=format)
if isinstance(t, (_GenericAlias, GenericAlias, Union)):
+ if isinstance(t, _LiteralGenericAlias):
+ # Unlike other generic aliases, Literal arguments aren't type
expressions
+ return t
if isinstance(t, GenericAlias):
args = tuple(
_make_forward_ref(arg, parent_fwdref=parent_fwdref) if
isinstance(arg, str) else arg
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]