Skip to content

Mypy fails to infer type parameters when composing generic functions with themselves #18864

Open
@LeeeeT

Description

@LeeeeT

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

  1. The first id function should be inferred as Callable[[T], list[T]]
  2. The second id function should be inferred as Callable[[list[T]], list[list[T]]]
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions