python / python/mypy

`singledispatch.register` doesn't typecheck registration of generic functions

Open
#13,040 4 comments 5 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

To Reproduce

Run mypy (no options) on this file:

⚠️ See below for actual MWE. This is preserved here to preserve the flow of discussion.

from typing import TypeVar
from functools import singledispatch

T = TypeVar("T")


@singledispatch
def f(x: T) -> T:
    return x


class A:
    pass


@f.register(A)
def _f_a(x: A) -> A:
    return x

Expected Behavior

No complaint. There's no bound on T, so I feel that _f_a should be acceptable.

Actual Behavior

mypybug.py:15: error: Dispatch type "A" must be subtype of fallback function first argument "T"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 0.961
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.10
  • Operating system and version: Debian Linux

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 mypy without options on the reproduced mypybug.py example and compare the diagnostic from the singledispatch.register call with the expected behavior. Trace the type-checking entry point for generic fallback functions and registration dispatch types; done means the example produces no complaint while incompatible registrations remain checked.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.