Incompatible types when calling generinc `__init__` from generic `@classmethod`
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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