@@ -276,8 +276,8 @@ def __getitem__(self, parameter):
276
276
if not issubclass (parameter , self .type_var .__constraints__ ):
277
277
raise TypeError ("%s is not a valid substitution for %s." %
278
278
(parameter , self .type_var ))
279
- if isinstance (parameter , TypeVar ):
280
- raise TypeError ("%s cannot be re-parameterized." % self . type_var )
279
+ if isinstance (parameter , TypeVar ) and parameter is not self . type_var :
280
+ raise TypeError ("%s cannot be re-parameterized." % self )
281
281
return self .__class__ (self .name , parameter ,
282
282
self .impl_type , self .type_checker )
283
283
@@ -398,12 +398,15 @@ def _eval_type(self, globalns, localns):
398
398
399
399
def _get_type_vars (self , tvars ):
400
400
if self .__type__ :
401
- _get_type_vars (self .__type__ , tvars )
401
+ _get_type_vars ([ self .__type__ ] , tvars )
402
402
403
403
def __repr__ (self ):
404
+ return self ._subs_repr ([], [])
405
+
406
+ def _subs_repr (self , tvars , args ):
404
407
r = super (_ClassVar , self ).__repr__ ()
405
408
if self .__type__ is not None :
406
- r += '[{}]' .format (_type_repr (self .__type__ ))
409
+ r += '[{}]' .format (_replace_arg (self .__type__ , tvars , args ))
407
410
return r
408
411
409
412
def __hash__ (self ):
@@ -703,9 +706,12 @@ def _get_type_vars(self, tvars):
703
706
_get_type_vars (self .__union_params__ , tvars )
704
707
705
708
def __repr__ (self ):
709
+ return self ._subs_repr ([], [])
710
+
711
+ def _subs_repr (self , tvars , args ):
706
712
r = super (_Union , self ).__repr__ ()
707
713
if self .__union_params__ :
708
- r += '[%s]' % (', ' .join (_type_repr ( t )
714
+ r += '[%s]' % (', ' .join (_replace_arg ( t , tvars , args )
709
715
for t in self .__union_params__ ))
710
716
return r
711
717
@@ -805,9 +811,12 @@ def _eval_type(self, globalns, localns):
805
811
return self .__class__ (p , _root = True )
806
812
807
813
def __repr__ (self ):
814
+ return self ._subs_repr ([], [])
815
+
816
+ def _subs_repr (self , tvars , args ):
808
817
r = super (_Tuple , self ).__repr__ ()
809
818
if self .__tuple_params__ is not None :
810
- params = [_type_repr ( p ) for p in self .__tuple_params__ ]
819
+ params = [_replace_arg ( p , tvars , args ) for p in self .__tuple_params__ ]
811
820
if self .__tuple_use_ellipsis__ :
812
821
params .append ('...' )
813
822
if not params :
@@ -898,6 +907,8 @@ def __init__(self, args=None, result=None, _root=False):
898
907
def _get_type_vars (self , tvars ):
899
908
if self .__args__ and self .__args__ is not Ellipsis :
900
909
_get_type_vars (self .__args__ , tvars )
910
+ if self .__result__ :
911
+ _get_type_vars ([self .__result__ ], tvars )
901
912
902
913
def _eval_type (self , globalns , localns ):
903
914
if self .__args__ is None and self .__result__ is None :
@@ -913,14 +924,17 @@ def _eval_type(self, globalns, localns):
913
924
return self .__class__ (args = args , result = result , _root = True )
914
925
915
926
def __repr__ (self ):
927
+ return self ._subs_repr ([], [])
928
+
929
+ def _subs_repr (self , tvars , args ):
916
930
r = super (_Callable , self ).__repr__ ()
917
931
if self .__args__ is not None or self .__result__ is not None :
918
932
if self .__args__ is Ellipsis :
919
933
args_r = '...'
920
934
else :
921
- args_r = '[%s]' % ', ' .join (_type_repr ( t )
935
+ args_r = '[%s]' % ', ' .join (_replace_arg ( t , tvars , args )
922
936
for t in self .__args__ )
923
- r += '[%s, %s]' % (args_r , _type_repr (self .__result__ ))
937
+ r += '[%s, %s]' % (args_r , _replace_arg (self .__result__ , tvars , args ))
924
938
return r
925
939
926
940
def __getitem__ (self , parameters ):
@@ -985,6 +999,16 @@ def _geqv(a, b):
985
999
return _gorg (a ) is _gorg (b )
986
1000
987
1001
1002
+ def _replace_arg (arg , tvars , args ):
1003
+ if hasattr (arg , '_subs_repr' ):
1004
+ return arg ._subs_repr (tvars , args )
1005
+ if isinstance (arg , TypeVar ):
1006
+ for i , tvar in enumerate (tvars ):
1007
+ if arg .__name__ == tvar .__name__ :
1008
+ return args [i ]
1009
+ return _type_repr (arg )
1010
+
1011
+
988
1012
def _next_in_mro (cls ):
989
1013
"""Helper for Generic.__new__.
990
1014
@@ -1115,17 +1139,29 @@ def _get_type_vars(self, tvars):
1115
1139
_get_type_vars (self .__parameters__ , tvars )
1116
1140
1117
1141
def __repr__ (self ):
1118
- if self .__origin__ is not None :
1119
- r = repr (self .__origin__ )
1120
- else :
1121
- r = super (GenericMeta , self ).__repr__ ()
1122
- if self .__args__ :
1123
- r += '[%s]' % (
1124
- ', ' .join (_type_repr (p ) for p in self .__args__ ))
1125
- if self .__parameters__ :
1126
- r += '<%s>' % (
1127
- ', ' .join (_type_repr (p ) for p in self .__parameters__ ))
1128
- return r
1142
+ if self .__origin__ is None :
1143
+ return super (GenericMeta , self ).__repr__ ()
1144
+ return self ._subs_repr ([], [])
1145
+
1146
+ def _subs_repr (self , tvars , args ):
1147
+ assert len (tvars ) == len (args )
1148
+ # Construct the chain of __origin__'s.
1149
+ current = self .__origin__
1150
+ orig_chain = []
1151
+ while current .__origin__ is not None :
1152
+ orig_chain .append (current )
1153
+ current = current .__origin__
1154
+ # Replace type variables in __args__ if asked ...
1155
+ str_args = []
1156
+ for arg in self .__args__ :
1157
+ str_args .append (_replace_arg (arg , tvars , args ))
1158
+ # ... then continue replacing down the origin chain.
1159
+ for cls in orig_chain :
1160
+ new_str_args = []
1161
+ for i , arg in enumerate (cls .__args__ ):
1162
+ new_str_args .append (_replace_arg (arg , cls .__parameters__ , str_args ))
1163
+ str_args = new_str_args
1164
+ return super (GenericMeta , self ).__repr__ () + '[%s]' % ', ' .join (str_args )
1129
1165
1130
1166
def __eq__ (self , other ):
1131
1167
if not isinstance (other , GenericMeta ):
@@ -1158,11 +1194,11 @@ def __getitem__(self, params):
1158
1194
raise TypeError (
1159
1195
"Parameters to Generic[...] must all be unique" )
1160
1196
tvars = params
1161
- args = None
1197
+ args = params
1162
1198
elif self is _Protocol :
1163
1199
# _Protocol is internal, don't check anything.
1164
1200
tvars = params
1165
- args = None
1201
+ args = params
1166
1202
elif self .__origin__ in (Generic , _Protocol ):
1167
1203
# Can't subscript Generic[...] or _Protocol[...].
1168
1204
raise TypeError ("Cannot subscript already-subscripted %s" %
0 commit comments