python / python/mypy

Incompatible singledispatch signature not detected

Open
#11,734 2 comments 1 reaction 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

This generates no error from mypy but fails at runtime:

from functools import singledispatch

@singledispatch
def f(x: object) -> int:
    return 1

@f.register
def _(x: int, y: str) -> int:
    return 2

print(f(1))  # No error reported, but fails at runtime

Another example:

from functools import singledispatch

@singledispatch
def f(x: object, y: str) -> None:
    y + ''

@f.register
def _(x: int, y: int) -> None:
    y + 1

print(f(1, 'x'))  # No error reported, but fails at runtime

Third example:

from functools import singledispatch

@singledispatch
def f(x: object, y: str) -> None:
    pass

@f.register
def _(x: int, yy: str) -> None:
    pass

print(f(1, y='x'))  # No error reported, but fails at runtime

We should check that the signatures of registered items are compatible with the generic one, except for the first argument.

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 three singledispatch examples and trace mypy's handling of registered functions and signature compatibility. Add regression coverage for positional and keyword calls, then verify that incompatible registered signatures produce errors while compatible ones remain accepted.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.