@overload is required when using @dispatch

Open
#3,853 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Start with the synthetic Python example in the issue and reproduce mypy's “'overload' decorator expected” error. Investigate how mypy handles decorators and what support for multipledispatch's @dispatch would require; done means the example type-checks without adding runtime @overload decorators.

Written by the indexing model from the issue text.

Description

bug false-positive

Is there a way to type-check code that uses @dispatch?

For example, consider the following synthetic example code:

from multipledispatch import dispatch

@dispatch(int)
def foo(x: int) -> None:
    print('int')

@dispatch(str)
def foo(x: str) -> None:
    print('str')

foo(1)  # Prints 'int'.
foo('a')  # Prints 'str'.

When I try to type-check this code with mypy, I get error: 'overload' decorator expected, because mypy does not recognize the @dispatch decorator.

Adding @overload before each @dispatch would satisfy mypy. However, it raises a run time error saying You should not call an overloaded function.. This makes sense, as the semantics of @overload are "this is only a signature, the real body is elsewhere.".

It seems what is needed is for mypy to directly recognize @dispatch...?

Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

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.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.