Overlapping overloads with default arguments? #10914
Unanswered
Sahil Jain (whoami730)
asked this question in
Q&A
Replies: 2 comments 4 replies
|
This is an unsound overlapping overload. Mypy does not detect all unsound overlapping overloads. You can fix the first example by eliminating the default argument in the The second example requires a bit more creativity to avoid the unsoundness: @overload
def func(param2: float, param: Literal[True]) -> str: ...
@overload
def func(*, param2: float = ..., param: Literal[False] = False) -> float: ...
@overload
def func(param2: float = ..., param: Literal[False] = ..., /) -> float: ...If you're comfortable with the unsoundness, you can suppress the error by adding a |
4 replies
|
For the second example -- # pyright: strict
from typing import overload, Literal
@overload
def func(*, param: Literal[True]) -> str: ...
@overload
def func(param2: float, param: Literal[True]) -> str : ...
@overload
def func(param2: float = ..., param: Literal[False] = ...) -> float : ...
def func(param2: float = 1e-8, param: bool = False) :
if param:
return str('plot_points')
else:
return float(param2)
reveal_type(func()) # float
reveal_type(func(0)) # float
reveal_type(func(param2=0)) # float
reveal_type(func(param=True)) # str
reveal_type(func(0, True)) # str
reveal_type(func(0, param=True)) # str
reveal_type(func(param2=0, param=True)) # str
reveal_type(func(param=False)) # float
reveal_type(func(0, False)) # float
reveal_type(func(0, param=False)) # float
reveal_type(func(param2=0, param=False)) # float
reveal_type(func(param=bool())) # str | float
reveal_type(func(0, bool())) # str | float
reveal_type(func(0, param=bool())) # str | float
reveal_type(func(param2=0, param=bool())) # str | float |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have the below source code :
It seems to error with
Overload 1 for "func" overlaps overload 2 and returns an incompatible type (reportOverlappingOverload), however it seems to pass with mypy without any errors.I was expecting this to work after updates to the typing spec.
The above one can however be fixed by removing the additional
= ...afterLiteral[X]types.Another related example is -
for which unfortunately we can't remove the
= ...due to non-default arguments not following default arguments (pyright does not report this error after the removal though). And therefore I have no solution in mind to avoid the errors.All reactions