python / python/mypy

Check for return type of overload implementation is too strict?

Open
#7,338 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

false-positive needs discussion priority-1-normal topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Consider this simplified example:

from typing import overload, Optional, List
  
@overload
def f(x: int) -> List[int]: ...
@overload
def f(x: None) -> List[Optional[int]]: ...

def f(x: Optional[int]) -> List[Optional[int]]:
    return [x]

Mypy currently complains about it that:

Overloaded function implementation cannot produce return type of signature 1

Although I understand why this happens, it is a bit annoying and can be perceived as a false positive. Maybe we should use is_overlapping_types() instead of is_subtype() for the check?

cc @Michael0x2a

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 reproducing the simplified overload example and trace the implementation return-type check in mypy. Compare the current is_subtype() check with the proposed is_overlapping_types() behavior, then determine whether the example should be accepted and validate the decision with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.