Skip to content
Discussion options

You must be logged in to vote

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

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Xoriun
Comment options

Answer selected by Xoriun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants