Skip to content

Overload based on optional argument #1326

Answered by hauntsaninja
plannigan asked this question in Q&A
Discussion options

You must be logged in to vote

Eric's answer isn't fully correct, since it doesn't permit thing("value", include_extra=True)

Here is something that should be exhaustively correct. It's a little bit annoying, but if you can change your API a little bit you have some opportunities to simplify.

from typing import overload, Literal, Tuple, Optional, Union

# this overload is the straightforward False case
@overload
def thing(value_1: str, value_2: Optional[str] = None, include_extra: Literal[False] = False) -> Tuple[str, str]: ...
# this overload is mostly straightforward, except that we can't include a default value for value_2 because arguments with default values must be at the end
# (if we included a default value for …

Replies: 3 comments 7 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@plannigan
Comment options

@erictraut
Comment options

@gandhis1
Comment options

@igorghi
Comment options

@erictraut
Comment options

Answer selected by plannigan
Comment options

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

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