Overload validation does not properly handle implied subtype relationships (e.g. int and float)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
I'm working to make pyright's overload validation work consistently with mypy's.
Mypy's overload validation behavior is spec'ed here: https://github.com/python/typing/issues/253#issuecomment-389262904
As part of this validation, mypy reports errors when there is overlap between two overloads that return different return types. The following test case shows four such examples. Mypy properly reports the error in three of the four cases, but it misses the case where there is an implied subtype relationship, as between int and float.
# pyright: strict
from typing import Literal, Union, overload
class Parent: ...
class Child(Parent): ...
# Test 1: Literal subtype
@overload
def foo1(x: Literal[3]) -> int: ...
@overload
def foo1(x: int) -> str: ...
# Test 2: Subclass subtype
@overload
def foo2(x: Child) -> str: ...
@overload
def foo2(x: Parent) -> int: ...
# Test 3: Implicit subtype
@overload
def foo3(x: int) -> str: ... # Mypy does not report error here
@overload
def foo3(x: float) -> int: ...
# Test 4: Union subtype
@overload
def foo4(x: int) -> str: ...
@overload
def foo4(x: Union[int, str]) -> int: ...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the four overload examples from the issue and compare mypy's diagnostics with the linked typing specification. Trace the overload-validation path responsible for subtype overlap checks, then verify that the int/float case is diagnosed consistently with the literal, subclass, and union cases.
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