python / python/mypy

TypeVar bound not respected in Generic class method overloads

Open
#18,419 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy chooses the wrong overload of a generic class method in the below code. str | Sequence[int] would match Sequence[ItemT] if ItemT was not bounded or constrained, but since it is constrained to non-str the overload for str | Sequence[ItemT] should be matched instead. The issue only affects generic class methods and not regular functions.

To Reproduce

from collections.abc import Sequence
from typing import Generic, TypeVar, overload

from typing_extensions import assert_type


T = TypeVar("T", covariant=True)
ItemT = TypeVar("ItemT", bound=int)  # OR TypeVar("ItemT", int, float)


class MyGeneric(Generic[T]):
    @overload
    def transform(self: "MyGeneric[str]") -> "MyGeneric[str]": ...

    @overload
    def transform(self: "MyGeneric[Sequence[ItemT]]") -> "MyGeneric[Sequence[ItemT]]": ...

    @overload
    def transform(self: "MyGeneric[str | Sequence[ItemT]]") -> "MyGeneric[str | Sequence[ItemT]]": ...

    def transform(self: "MyGeneric[str | Sequence[ItemT]]") -> "MyGeneric[str | Sequence[ItemT]]": ...  # type: ignore


assert_type(MyGeneric[str]().transform(), MyGeneric[str])
assert_type(MyGeneric[Sequence[int]]().transform(), MyGeneric[Sequence[int]])
assert_type(MyGeneric[str | Sequence[int]]().transform(), MyGeneric[str | Sequence[int]])
# error: Expression is of type "MyGeneric[Sequence[object]]", not "MyGeneric[str | Sequence[int]]"

# ---
# The equivalent functions do not have the same issue

@overload
def transform(x: "MyGeneric[str]") -> "MyGeneric[str]": ...

@overload
def transform(x: "MyGeneric[Sequence[ItemT]]") -> "MyGeneric[Sequence[ItemT]]": ...

@overload
def transform(x: "MyGeneric[str | Sequence[ItemT]]") -> "MyGeneric[str | Sequence[ItemT]]": ...

def transform(x: "MyGeneric[str | Sequence[ItemT]]") -> "MyGeneric[str | Sequence[ItemT]]": ...  # type: ignore


assert_type(transform(MyGeneric[str]()), MyGeneric[str])
assert_type(transform(MyGeneric[Sequence[int]]()), MyGeneric[Sequence[int]])
assert_type(transform(MyGeneric[str | Sequence[int]]()), MyGeneric[str | Sequence[int]])

Your Environment

  • Mypy version used: 1.14.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.13.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied reproduction with mypy 1.14.1 and confirming the incorrect assert_type result for the generic class method. Trace overload resolution for generic class methods, then add or update coverage so the bound or constrained TypeVar selects the expected overload while the equivalent functions remain correct.

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
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.