Open
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
id
function should be inferred asCallable[[T], list[T]]
- The second
id
function 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