mypy doesn't escape typevars from generics in self-type
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided generic self-type reproducer under mypy v1.14 with --strict and inspect how reveal_type resolves XCallable[[T], None].f(). Trace the type inference path responsible for the result, and consider the issue complete when the returned callable preserves its generic type instead of becoming (Never) -> Never.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100