The example code of what I am trying to ask is below.
None of the examples on the internet try to overload argument value as such.
One of the argument is a bool value and I want to overload a method based on the bool value rather than the usual, argument type.
from typing import overload, Union
@overload
def myfunc(arg: bool = True)-> str: ...
@overload
def myfunc(arg: bool = False) -> int: ...
def myfunc(arg: bool) -> Union[int, str]:
if arg:
return "something"
else:
return 0
Is the overloading code in the above example code correct ?
Can you give an example/blog/source that mentions this kind of overloading, since I couldn't find anything in Python docs and pep-484
I found one probable way of doing it is with typing.Literal
as used in latest python docs (since python v3.8)
from typing import overload, Union, Literal
@overload
def myfunc(arg: Literal[True]) -> str: ...
@overload
def myfunc(arg: Literal[False]) -> int: ...
def myfunc(arg: bool) -> Union[int, str]:
if arg:
return "something"
else:
return 0
But I cannot move to python 3.8 just yet as I am working on production code that is still on python 3.6, soon upgrading to 3.7 at best.
Hence I am still looking for answers on how to achieve this in python 3.6