diff --git a/mypy/join.py b/mypy/join.py index a8c9910e60bb..6e21171378ab 100644 --- a/mypy/join.py +++ b/mypy/join.py @@ -297,6 +297,8 @@ def visit_erased_type(self, t: ErasedType) -> ProperType: return self.s def visit_type_var(self, t: TypeVarType) -> ProperType: + if is_subtype(self.s, t): + return t if isinstance(self.s, TypeVarType): if self.s.id == t.id: if self.s.upper_bound == t.upper_bound: diff --git a/mypy/subtypes.py b/mypy/subtypes.py index 66d7a95eb425..8f2287e4ce3d 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -312,6 +312,19 @@ def _is_subtype( # ErasedType as we do for non-proper subtyping. return True + if isinstance(right, TypeVarType) and right.values: + if isinstance(left, TypeVarType) and left.id == right.id: + return True + if proper_subtype: + return all( + is_proper_subtype(orig_left, v, subtype_context=subtype_context) + for v in right.values + ) + else: + return all( + is_subtype(orig_left, v, subtype_context=subtype_context) for v in right.values + ) + if isinstance(right, UnionType) and not isinstance(left, UnionType): # Normally, when 'left' is not itself a union, the only way # 'left' can be a subtype of the union 'right' is if it is a