python / python/mypy

mypy tries to infer the type parameters of a generic class even if they are explicitly given

Open
#17,916 0 comments 0 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

mypy tries to infer the type parameters of a generic class even if they are explicitly given.

To Reproduce

from typing import overload


class Frobnicator[T]:  # Using a TypeVar gives identical results
    @overload
    def __init__(self, param: T) -> None: ...
    @overload  # Removing `param` in this case gives identical results
    def __init__(self: 'Frobnicator[None]', param: None = None) -> None: ... 
    def __init__(self, param: T | None = None) -> None:
        ...

Expected Behavior

I expect the following to produce a "missing argument" error (like Pyright 1.1.384 does).

f = Frobnicator[int]()

Actual Behavior

reveal_type(f) shows Frobnicator[None]

Your Environment

The behavior is identical for mypy v1.11.1, v1.11.2 and the master branch, with both Python 3.12 and 3.13. I tried it on my local Arch + pyenv based setup and, using TypeVar, in https://mypy-play.net

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 Frobnicator example on the current mypy master branch and compare its result with the expected missing-argument error. Trace the type inference and overload checking involved in the explicit Frobnicatorint call; done means the call reports the missing argument instead of inferring Frobnicator[None].

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.