diff --git a/mypy/constraints.py b/mypy/constraints.py index ebf48f1dfec8..040bcff8c99d 100644 --- a/mypy/constraints.py +++ b/mypy/constraints.py @@ -513,6 +513,10 @@ def visit_instance(self, template: Instance) -> List[Constraint]: return infer_constraints(template, mypy.typeops.tuple_fallback(actual), self.direction) + elif isinstance(actual, TypeVarType): + if not actual.values: + return infer_constraints(template, actual.upper_bound, self.direction) + return [] else: return [] diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index d04ba28ccb03..65fb30a3547f 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -2807,6 +2807,27 @@ class MyClass: [builtins fixtures/isinstance.pyi] [typing fixtures/typing-full.pyi] +[case testProtocolAndTypeVariableSpecialCase] +from typing import TypeVar, Iterable, Optional, Callable, Protocol + +T_co = TypeVar('T_co', covariant=True) + +class SupportsNext(Protocol[T_co]): + def __next__(self) -> T_co: ... + +N = TypeVar("N", bound=SupportsNext, covariant=True) + +class SupportsIter(Protocol[T_co]): + def __iter__(self) -> T_co: ... + +def f(i: SupportsIter[N]) -> N: ... + +I = TypeVar('I', bound=Iterable) + +def g(x: I, y: Iterable) -> None: + f(x) + f(y) + [case testMatchProtocolAgainstOverloadWithAmbiguity] from typing import TypeVar, Protocol, Union, Generic, overload