44import types
55import typing
66
7+ from typing import _GenericAlias as typing_GenericAlias # type: ignore [attr-defined] # noqa: PLC2701
8+
9+
710from . import _eval_typing
11+ from . import _typing_inspect
812
913if typing .TYPE_CHECKING :
1014 from typing import Any
@@ -44,8 +48,17 @@ def dump(self, *, _level: int = 0):
4448 b .dump (_level = _level + 1 )
4549
4650
51+ def substitute (ty , args ):
52+ if ty in args :
53+ return args [ty ]
54+ elif isinstance (ty , (typing_GenericAlias , types .GenericAlias )):
55+ return ty .__origin__ [* [substitute (t , args ) for t in ty .__args__ ]]
56+ else :
57+ return ty
58+
59+
4760def box (cls : type [Any ]) -> Boxed :
48- def _box (cls : type [Any ], args : dict [str , Any ]) -> Boxed :
61+ def _box (cls : type [Any ], args : dict [Any , Any ]) -> Boxed :
4962 boxed_bases : list [Boxed ] = []
5063
5164 orig_bases = cls .__dict__ .get ("__orig_bases__" )
@@ -60,14 +73,12 @@ def _box(cls: type[Any], args: dict[str, Any]) -> Boxed:
6073
6174 if issubclass (base , typing .Generic ):
6275 if base_params := getattr (base , "__parameters__" , None ):
76+ # obase should be _GenericAlias...
6377 boxed_args = {}
6478 for param , arg in zip (
6579 base_params , obase .__args__ , strict = True
6680 ):
67- if arg in args :
68- boxed_args [param ] = args [arg ]
69- else :
70- boxed_args [param ] = arg
81+ boxed_args [param ] = substitute (arg , args )
7182 else :
7283 boxed_args = {}
7384
@@ -85,7 +96,7 @@ def _box(cls: type[Any], args: dict[str, Any]) -> Boxed:
8596 cls = cls .__origin__
8697 else :
8798 if params := getattr (cls , "__parameters__" , None ):
88- args = {p : p for p in params }
99+ args = {p : _typing_inspect . param_default ( p ) for p in params }
89100 else :
90101 args = {}
91102
@@ -120,7 +131,7 @@ def merge_boxed_mro(seqs: list[list[Boxed]]) -> list[Boxed]:
120131
121132def _compute_mro (C : Boxed ) -> list [Boxed ]:
122133 return merge_boxed_mro (
123- [[C ]] + list ( map ( _compute_mro , C .bases )) + [list (C .bases )]
134+ [[C ]] + [ _compute_mro ( b ) for b in C .bases ] + [list (C .bases )]
124135 )
125136
126137
0 commit comments