Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to write typing.overload decorator for bool arguments by value

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

like image 678
dumbPy Avatar asked Dec 16 '19 15:12

dumbPy


People also ask

How do you overload a function in Python?

Python does not support function overloading. Suppose we still want to use the feature of functional overloading. In that case, we can set the default values of parameters in the method as None, which won't give an error if that specific value is not passed as an argument while calling the function.

What is typing overload Python?

2021-05-29. Sometimes the types of several variables are related, such as “if x is type A, y is type B, else y is type C”. Basic type hints cannot describe such relationships, making type checking cumbersome or inaccurate. We can instead use @typing. overload to represent type relationships properly.

Does Python enforce type hints?

Python will always remain a dynamically typed language. However, PEP 484 introduced type hints, which make it possible to also do static type checking of Python code. Unlike how types work in most other statically typed languages, type hints by themselves don't cause Python to enforce types.

What is overload decorator in Python?

The @overload decorator allows you to define alternate implementations of a function, specialized by argument type(s). A function with the same name must already exist in the local namespace.


1 Answers

Building off the correct accepted answer, @overloading for default (keyword) arguments can be achieved as follows:

from typing import overload, Union, Literal

@overload
def myfunc(arg: Literal[True] = True) -> str: ...

@overload
def myfunc(arg: Literal[False]) -> int: ...

def myfunc(arg: bool =  True) -> Union[int, str]:
    if arg: return "something"
    else: return 0

The first overload (with Literal[True]) is used for type-checking function calls myfunc(True) and myfunc(), while the second overload (with Literal[False]) is used for type-checking the function call myfunc(False).

like image 197
Jasha Avatar answered Nov 15 '22 19:11

Jasha