Skip to content

Commit 3d51987

Browse files
committed
Ignore both use- and declration variance
1 parent 7dd59ce commit 3d51987

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/ir/type_utils.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def _find_candidate_type_args(t_param: tp.TypeParameter,
8787
types,
8888
get_subtypes,
8989
type_var_map={},
90-
ignore_use_variance=False):
90+
ignore_variance=False):
9191

9292
bound = None
9393
if t_param.bound:
@@ -107,7 +107,7 @@ def _find_candidate_type_args(t_param: tp.TypeParameter,
107107
if not base_targ:
108108
return None
109109

110-
if t_param.is_invariant():
110+
if t_param.is_invariant() or ignore_variance:
111111
t_args = [base_targ]
112112
elif t_param.is_covariant():
113113
t_args = _find_types(
@@ -118,7 +118,7 @@ def _find_candidate_type_args(t_param: tp.TypeParameter,
118118
base_targ, types,
119119
not get_subtypes, True, bound, concrete_only=True)
120120

121-
if not base_targ.is_wildcard() or ignore_use_variance:
121+
if not base_targ.is_wildcard() or ignore_variance:
122122
return t_args
123123
if base_targ.is_covariant():
124124
new_types = _find_types(
@@ -138,7 +138,7 @@ def _find_candidate_type_args(t_param: tp.TypeParameter,
138138

139139

140140
def _construct_related_types(etype: tp.ParameterizedType, types, get_subtypes,
141-
ignore_use_variance=False):
141+
ignore_variance=False):
142142
type_var_map = OrderedDict()
143143
if etype.name == 'Array':
144144
types = [t for t in types
@@ -201,7 +201,7 @@ def _construct_related_types(etype: tp.ParameterizedType, types, get_subtypes,
201201
t_args = _find_candidate_type_args(t_param, etype.type_args[i],
202202
types, get_subtypes,
203203
type_var_map,
204-
ignore_use_variance)
204+
ignore_variance)
205205
if not t_args:
206206
# We were not able to construct a subtype of the given
207207
# parameterized type. Therefore, we give back the given
@@ -221,7 +221,7 @@ def to_type(stype, types):
221221

222222

223223
def _find_types(etype, types, get_subtypes, include_self, bound=None,
224-
concrete_only=False, ignore_use_variance=False):
224+
concrete_only=False, ignore_variance=False):
225225

226226
# Otherwise, if we want to find the supertypes of a given type, `bound`
227227
# is interpreted a greatest bound.
@@ -242,7 +242,7 @@ def _find_types(etype, types, get_subtypes, include_self, bound=None,
242242
if isinstance(etype, tp.ParameterizedType):
243243
t_set.add(_construct_related_types(
244244
etype, types, get_subtypes,
245-
ignore_use_variance=ignore_use_variance))
245+
ignore_variance=ignore_variance))
246246
if include_self:
247247
t_set.add(etype)
248248
else:
@@ -255,10 +255,10 @@ def _find_types(etype, types, get_subtypes, include_self, bound=None,
255255

256256
def find_subtypes(etype, types, include_self=False, bound=None,
257257
concrete_only=False,
258-
ignore_use_variance=False):
258+
ignore_variance=False):
259259
return _find_types(etype, types, get_subtypes=True,
260260
include_self=include_self, concrete_only=concrete_only,
261-
ignore_use_variance=ignore_use_variance)
261+
ignore_variance=ignore_variance)
262262

263263

264264
def find_supertypes(etype, types, include_self=False, bound=None,
@@ -594,9 +594,10 @@ def _compute_type_variable_assignments(
594594
# to prevent creating invalid types, e.g.,
595595
# * bound: Foo<X, X>
596596
# * X is assigned to out Number
597-
# * Prevent creating Foo<Long, Number>
597+
# * class Bar<X, T extends Foo<X, X>>
598+
# * Prevent creating Bar<out Number, Foo<Long, Number>>
598599
a_types = find_subtypes(bound, types, True,
599-
ignore_use_variance=True)
600+
ignore_variance=True)
600601
for i, t in enumerate(a_types):
601602
if isinstance(t, tp.ParameterizedType):
602603
a_types[i] = t.to_variance_free()
@@ -657,7 +658,7 @@ def _compute_type_variable_assignments(
657658
types = [t for t in types if t != c]
658659
cls_type, _ = instantiate_type_constructor(
659660
cls_type, types, True, type_var_map,
660-
None if variance_choices is None else {}
661+
None if variance_choices is None else {},
661662
)
662663
variance = _get_type_arg_variance(t_param, variance_choices)
663664
t_arg = cls_type
@@ -736,7 +737,8 @@ def instantiate_parameterized_function(
736737
types = _get_available_types(None, types, only_regular, primitives=False)
737738
_, type_var_map = _compute_type_variable_assignments(
738739
type_parameters, types, type_var_map=type_var_map,
739-
variance_choices=None, for_type_constructor=False)
740+
variance_choices=None, for_type_constructor=False,
741+
)
740742
return type_var_map
741743

742744

0 commit comments

Comments
 (0)