@@ -121,6 +121,19 @@ def get_supertypes(self):
121
121
stack .append (supertype )
122
122
return visited
123
123
124
+ def substitute_type_args (self , type_map ,
125
+ cond = lambda t : t .has_type_variables ()):
126
+ t = type_map .get (self )
127
+ if t is None or cond (t ):
128
+ # Perform type substitution on the bound of the current type variable.
129
+ if self .is_type_var () and self .bound is not None :
130
+ new_bound = self .bound .substitute_type_args (type_map , cond )
131
+ return TypeParameter (self .name , self .variance , new_bound )
132
+ # The type parameter does not correspond to an abstract type
133
+ # so, there is nothing to substitute.
134
+ return self
135
+ return t
136
+
124
137
def not_related (self , other : Type ):
125
138
return not (self .is_subtype (other ) or other .is_subtype (self ))
126
139
@@ -343,6 +356,18 @@ def get_type_variables(self, factory):
343
356
else :
344
357
return {}
345
358
359
+ def substitute_type_args (self , type_map ,
360
+ cond = lambda t : t .has_type_variables ()):
361
+ if self .bound is not None :
362
+ new_bound = self .bound .substitute_type_args (type_map , cond )
363
+ return WildCardType (new_bound , variance = self .variance )
364
+ t = type_map .get (self )
365
+ if t is None or cond (t ):
366
+ # The bound does not correspond to abstract type
367
+ # so there is nothing to substitute
368
+ return self
369
+ return t
370
+
346
371
def get_bound_rec (self ):
347
372
if not self .bound :
348
373
return None
@@ -385,42 +410,8 @@ def is_primitive(self):
385
410
return False
386
411
387
412
388
- def _get_type_substitution (etype , type_map ,
389
- cond = lambda t : t .has_type_variables ()):
390
- if etype .is_parameterized ():
391
- return substitute_type_args (etype , type_map , cond )
392
- if etype .is_wildcard () and etype .bound is not None :
393
- new_bound = _get_type_substitution (etype .bound , type_map , cond )
394
- return WildCardType (new_bound , variance = etype .variance )
395
- t = type_map .get (etype )
396
- if t is None or cond (t ):
397
- # Perform type substitution on the bound of the current type variable.
398
- if etype .is_type_var () and etype .bound is not None :
399
- new_bound = _get_type_substitution (etype .bound , type_map , cond )
400
- return TypeParameter (etype .name , etype .variance , new_bound )
401
- # The type parameter does not correspond to an abstract type
402
- # so, there is nothing to substitute.
403
- return etype
404
- return t
405
-
406
-
407
- def substitute_type_args (etype , type_map ,
408
- cond = lambda t : t .has_type_variables ()):
409
- assert etype .is_parameterized ()
410
- type_args = []
411
- for t_arg in etype .type_args :
412
- type_args .append (_get_type_substitution (t_arg , type_map , cond ))
413
- new_type_map = {
414
- tp : type_args [i ]
415
- for i , tp in enumerate (etype .t_constructor .type_parameters )
416
- }
417
- type_con = perform_type_substitution (
418
- etype .t_constructor , new_type_map , cond )
419
- return ParameterizedType (type_con , type_args )
420
-
421
-
422
413
def substitute_type (t , type_map ):
423
- return _get_type_substitution ( t , type_map , lambda t : False )
414
+ return t . substitute_type_args ( type_map , lambda t : False )
424
415
425
416
426
417
def perform_type_substitution (etype , type_map ,
@@ -443,7 +434,7 @@ class X<T> : Y<Z<T>>()
443
434
supertypes = []
444
435
for t in etype .supertypes :
445
436
if t .is_parameterized ():
446
- supertypes .append (substitute_type_args (t , type_map ))
437
+ supertypes .append (t . substitute_type_args (type_map ))
447
438
else :
448
439
supertypes .append (t )
449
440
type_params = []
@@ -682,6 +673,18 @@ def get_type_variables(self, factory) -> Dict[TypeParameter, Set[Type]]:
682
673
continue
683
674
return type_vars
684
675
676
+ def substitute_type_args (self , type_map , cond = lambda t : t .has_type_variables ()):
677
+ type_args = []
678
+ for t_arg in self .type_args :
679
+ type_args .append (t_arg .substitute_type_args (type_map , cond ))
680
+ new_type_map = {
681
+ tp : type_args [i ]
682
+ for i , tp in enumerate (self .t_constructor .type_parameters )
683
+ }
684
+ type_con = perform_type_substitution (
685
+ self .t_constructor , new_type_map , cond )
686
+ return ParameterizedType (type_con , type_args )
687
+
685
688
@property
686
689
def can_infer_type_args (self ):
687
690
return self ._can_infer_type_args
0 commit comments