-
|
In the following code, there is a bool argument with default value from typing import Literal, overload
@overload
def func(a: Literal[True]) -> int: ...
@overload
def func(a: Literal[False]) -> str: ...
def func(a: bool = False) -> int | str:
return 1 if a else 'ssdf'This works as expected as long as this optional argument is used when calling the function, but gives an a: int = func(True) # fine
b: str = func(False) # fine
c: str = func() # errorOf course, adding the overload @overload
def func() -> str: ...solves the issue. Now, is this third overload something that pyright could/should imply by the provided default argument or does leaving it out violate some official rule for type checkers? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi, you need to add the default value to the overloaded signature as well. Otherwise there is no option for zero arguments: from typing import Literal, overload
@overload
def func(a: Literal[True]) -> int: ...
# Default case:
@overload
def func(a: Literal[False] = False) -> str: ...
def func(a: bool = False) -> int | str:
return 1 if a else 'ssdf'
a: int = func(True) # fine
b: str = func(False) # fine
c: str = func() # fine |
Beta Was this translation helpful? Give feedback.
Hi, you need to add the default value to the overloaded signature as well. Otherwise there is no option for zero arguments: