python / python/mypy

Incompatible types when calling generinc `__init__` from generic `@classmethod`

Open
#17,937 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

When calling a generic __init__ from a generic @classmethod on a class that is itself not generic, mypy reports incompatible types.

To Reproduce

from typing import TypeVar

T = TypeVar("T")


class Foo:
    @classmethod
    def make_foo(cls, arg: T) -> None:
        cls(arg)

    def __init__(self, arg: T) -> None:
        pass

Expected Behavior

mypy reports no errors.

Actual Behavior

error: Argument 1 to "Foo" has incompatible type "T@make_foo"; expected "T@__init__"

Your Environment

  • Mypy version used: mypy 1.11.2 (compiled: yes)
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: Python 3.11.7

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 with the provided Python reproduction and run it under mypy to confirm the mismatch between T@make_foo and T@init. Trace generic type inference for the classmethod call to Foo(arg), then add a regression test showing that mypy reports no errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.