python / python/mypy

False positive error "incompatible type" calling super() on a generic class with TypeVar constraints

Open
#14,774 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Reduced test case (mypy-playground):

from typing import Generic, TypeVar

T = TypeVar("T", float, int)


class C(Generic[T]):
    def __init__(self, i: T) -> None:
        self.i: T = i


class B(C[T]):
    def __init__(self, i: T) -> None:
        super().__init__(i)
main.py: note: In member "__init__" of class "B":
main.py:13:26: error: Argument 1 to "__init__" of "C" has incompatible type "float"; expected "T"  [arg-type]
main.py:13:26: error: Argument 1 to "__init__" of "C" has incompatible type "int"; expected "T"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Originally posted by @leandro-lucarella-frequenz in https://github.com/python/mypy/issues/13566#issuecomment-1443582479 (this is a very similar issue but not exactly the same)

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 reduced Python example from the issue with mypy and compare its diagnostics with the expected behavior for the constrained TypeVar. Trace the handling of generic base classes and the super() call, then add or update a regression test so this example no longer reports incompatible types while genuine errors remain diagnosed.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.