-
I have the following code from typing import Literal, overload
@overload
def f1(model: str, convert: Literal[False] = ...) -> int: ...
@overload
def f1(model: str, convert: Literal[True] = ...) -> str: ...
@overload
def f1(model: str, convert: bool = ...) -> int | str: ...
def f1(model: str, convert: bool = True) -> int | str:
...
reveal_type(f1("")) # int -> ko
reveal_type(f1("", convert=True)) # str -> okt
reveal_type(f1("", convert=False)) # int -> ok Basically, I have two overloads for I guess it would be very complicated to extract the default value from the implementation, is there a way to have the right type deduce? I can think of two ways, but I am not sure if one is better:
Is there a preferred? Maybe there is a better way then those two? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You've specified three overloads with default values for @overload
def f1(model: str, convert: Literal[False]) -> int: ... |
Beta Was this translation helpful? Give feedback.
You've specified three overloads with default values for
convert
. Only two of them should have a default value specified. The overload withLiteral[False]
should not include a= ...
becauseLiteral[False]
is not a valid default value based on your implementation.