pyright icon indicating copy to clipboard operation
pyright copied to clipboard

No error when no overload matches

Open vMarcelino opened this issue 6 years ago • 3 comments

There is no error nor warning when no overload matches the called function

To Reproduce

from typing import Union, TypeVar, overload, Tuple, Optional
T = TypeVar('T')


@overload
def mouse_event(x1: int, y1: int) -> int:
    ...


@overload
def mouse_event(x1: int, y1: int, x2: int, y2: int) -> Tuple[int, int]:
    ...


def mouse_event(x1: int, 
                y1: int, 
                x2: Optional[int] = None, 
                y2: Optional[int] = None) -> Union[int, Tuple[int, int]]:
    if x2 is None and y2 is None:
        return (x1 + y1)
    elif x2 is not None and y2 is not None:
        return (x2, y2)
    else:
        raise TypeError("Bad arguments")


foo = mouse_event(1, 2, 3) # <-- No error

Expected behavior foo line should have a warning or error from pyright. Something like what mypy outputs:

error: No overload variant of "mouse_event" matches argument types "int", "int", "int"
 note: Possible overload variants: 
 note:     def mouse_event(x1: int, y1: int) -> int
 note:     def mouse_event(x1: int, y1: int, x2: int, y2: int) -> Tuple[int, int]

vMarcelino avatar May 13 '20 23:05 vMarcelino

Thanks for the bug report.

You're correct. I misinterpreted the language in PEP 484 and assumed that the type checker should honor the final (non-overloaded) version of the function, but in re-reading the PEP, it's quite clear on this matter. This is different from other languages I'm familiar with that support concepts similar to overloads.

erictraut avatar May 14 '20 00:05 erictraut

This will be fixed in the next release of pyright.

erictraut avatar May 14 '20 05:05 erictraut

This is now addressed in Pyright 1.1.37, which I just published.

erictraut avatar May 16 '20 16:05 erictraut