@@ -407,6 +407,17 @@ def inner(*args, **kwds):
407407 return decorator
408408
409409
410+ def _rebuild_generic_alias (alias : GenericAlias , args : tuple [object , ...]) -> GenericAlias :
411+ is_unpacked = alias .__unpacked__
412+ if _should_unflatten_callable_args (alias , args ):
413+ t = alias .__origin__ [(args [:- 1 ], args [- 1 ])]
414+ else :
415+ t = alias .__origin__ [args ]
416+ if is_unpacked :
417+ t = Unpack [t ]
418+ return t
419+
420+
410421def _deprecation_warning_for_no_type_params_passed (funcname : str ) -> None :
411422 import warnings
412423
@@ -454,25 +465,20 @@ def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=f
454465 _make_forward_ref (arg ) if isinstance (arg , str ) else arg
455466 for arg in t .__args__
456467 )
457- is_unpacked = t .__unpacked__
458- if _should_unflatten_callable_args (t , args ):
459- t = t .__origin__ [(args [:- 1 ], args [- 1 ])]
460- else :
461- t = t .__origin__ [args ]
462- if is_unpacked :
463- t = Unpack [t ]
468+ else :
469+ args = t .__args__
464470
465471 ev_args = tuple (
466472 _eval_type (
467473 a , globalns , localns , type_params , recursive_guard = recursive_guard ,
468474 format = format , owner = owner ,
469475 )
470- for a in t . __args__
476+ for a in args
471477 )
472478 if ev_args == t .__args__ :
473479 return t
474480 if isinstance (t , GenericAlias ):
475- return GenericAlias ( t . __origin__ , ev_args )
481+ return _rebuild_generic_alias ( t , ev_args )
476482 if isinstance (t , Union ):
477483 return functools .reduce (operator .or_ , ev_args )
478484 else :
@@ -2404,7 +2410,7 @@ def _strip_annotations(t):
24042410 stripped_args = tuple (_strip_annotations (a ) for a in t .__args__ )
24052411 if stripped_args == t .__args__ :
24062412 return t
2407- return GenericAlias ( t . __origin__ , stripped_args )
2413+ return _rebuild_generic_alias ( t , stripped_args )
24082414 if isinstance (t , Union ):
24092415 stripped_args = tuple (_strip_annotations (a ) for a in t .__args__ )
24102416 if stripped_args == t .__args__ :
0 commit comments