Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,15 @@ def visit_erased_type(self, t: ErasedType) -> ProperType:
return self.s

def visit_type_var(self, t: TypeVarType) -> ProperType:
if isinstance(self.s, TypeVarType) and self.s.id == t.id:
if self.s.upper_bound == t.upper_bound:
return self.s
return self.s.copy_modified(upper_bound=join_types(self.s.upper_bound, t.upper_bound))
if isinstance(self.s, TypeVarType):
if self.s.id == t.id:
if self.s.upper_bound == t.upper_bound:
return self.s
return self.s.copy_modified(
upper_bound=join_types(self.s.upper_bound, t.upper_bound)
)
# Fix non-commutative joins
return get_proper_type(join_types(self.s.upper_bound, t.upper_bound))
else:
return self.default(self.s)

Expand Down
29 changes: 29 additions & 0 deletions mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,35 @@ def test_join_type_type_type_var(self) -> None:
self.assert_join(self.fx.type_a, self.fx.t, self.fx.o)
self.assert_join(self.fx.t, self.fx.type_a, self.fx.o)

def test_join_type_var_bounds(self) -> None:
tvar1 = TypeVarType(
"tvar1",
"tvar1",
TypeVarId(-100),
[],
self.fx.o,
AnyType(TypeOfAny.from_omitted_generics),
INVARIANT,
)
any_type = AnyType(TypeOfAny.special_form)
tvar2 = TypeVarType(
"tvar2",
"tvar2",
TypeVarId(-101),
[],
upper_bound=UnionType(
[
TupleType([any_type], self.fx.std_tuple),
TupleType([any_type, any_type], self.fx.std_tuple),
]
),
default=AnyType(TypeOfAny.from_omitted_generics),
variance=INVARIANT,
)

self.assert_join(tvar1, tvar2, self.fx.o)
self.assert_join(tvar2, tvar1, self.fx.o)

# There are additional test cases in check-inference.test.

# TODO: Function types + varargs and default args.
Expand Down