basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

validate overload implementations

Open KotlinIsland opened this issue 1 year ago • 0 comments

simple scenario:

@overload
def f(a: str) -> int: ...
@overload
def f(a: int) -> str: ...
def f(a):
    return a

more realistic scenario:

from typing import overload
@overload
def f(a: int) -> int: ...
@overload
def f(*, b: str) -> str: ...
def f(a: int | None=None, b: str=""):
    if a:
        return a + 1
    return b

So this will not show the error of f(0) # ""

So the solution is to analyze the function for each overload, merging the overload and the defaults from the impl:

def f(a: int | None=None, b: str="") -> int:
    if a:
        return a + 1
    return b  # error: expected "int", found "str"
# AND
def f(a: None=None, b: str="") -> str:
    if a: # error: condition is always false
        return a + 1
    return b

This way we can collect the errors and work properly.

only problem is the "always true" errors we will run into, maybe we can inject a fake type into the parameters or something.

KotlinIsland avatar Jun 20 '24 06:06 KotlinIsland