mypy doesn't escape typevars from generics in self-type #18400
Open
Description
Bug Report
Passing a callable to another which matches the typevar with another typevar doesn't allow passing them into the return type. (if that makes no sense, the reproducer below is simple)
To Reproduce
from typing import Generic, TypeVar, Callable
T = TypeVar("T")
V = TypeVar("V")
class X(Generic[T]):
def f(self: X[Callable[[V], None]]) -> Callable[[V], V]:
def inner_f(v: V) -> V:
return v
return inner_f
reveal_type(X[Callable[[T], None]]().f()) # N: Revealed type is "def (Never) -> Never"
# ^ this should keep the generic
Expected Behavior
I don't expect a Never
.
Your Environment
Checked on mypy playground.
- Mypy version used: v1.14
- Mypy command-line flags:
--strict
- Mypy configuration options from
mypy.ini
(and other config files): N/A - Python version used: 3.12