Mypy fails to infer type parameters when composing generic functions with themselves
Open
Nobody has claimed this yet.
bug
topic-type-variables
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy incorrectly infers Never types when the same generic function is used in multiple argument positions of a higher-order generic function, preventing proper composition of identical generic functions.
To Reproduce
from collections.abc import Callable
def compose[A, B, C](first: Callable[[A], B], second: Callable[[B], C]) -> Callable[[A], C]: ...
def id[T](value: T) -> list[T]: ... # any generic would do
compose(id, id)
# Argument 1 to "compose" has incompatible type "Callable[[T], list[T]]"; expected "Callable[[Never], Never]"
# Argument 2 to "compose" has incompatible type "Callable[[T], list[T]]"; expected "Callable[[Never], Never]"
Expected Behavior
- The first
idfunction should be inferred asCallable[[T], list[T]] - The second
idfunction should be inferred asCallable[[list[T]], list[list[T]]] - The resulting composition should be
Callable[[T], list[list[T]]]
Actual Behavior
Mypy fails to infer type parameters.
Your Environment
- Mypy version used: 1.15.0
- Mypy command-line flags:
--strict - Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.13
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 reproducing the compose(id, id) example with mypy 1.15.0, Python 3.13, and --strict. Trace generic type inference for the two callable argument positions; done means the expected nested callable type is inferred without Never errors and a regression test covers the case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100