Suppose the following code:
from typing import Union
def invert(value: Union[str, int]) -> Union[int, str]:
if isinstance(value, str):
return int(value)
elif isinstance(value, int):
return str(value)
else:
raise ValueError("value must be 'int' or 'str'")
It is easily seen that a str
input leads to an int
output and vice versa. Is there a way to specify the return type so that it encodes this inverse relationship?
Here's how you can add type hints to our function: Add a colon and a data type after each function parameter. Add an arrow ( -> ) and a data type after the function to specify the return data type.
In a type hint, if we specify a type (class), then we mark the variable as containing an instance of that type. To specify that a variable instead contains a type, we need to use type[Cls] (or the old syntax typing. Type ).
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.
They are used to add types to variables, parameters, function arguments as well as their return values, class attributes, and methods. Adding type hints has no runtime effect: these are only hints and are not enforced on their own. If you worry about them, do remember that the interpreter always ignores them.
There isn't really a natural way to specify a conditional type hint in Python at the moment.
That said, in your particular case, you can use @overload
to express what you're trying to do:
from typing import overload, Union
# Body of overloads must be empty
@overload
def invert(value: str) -> int: ...
@overload
def invert(value: int) -> str: ...
# Implementation goes last, without an overload.
# Adding type hints here are optional -- if they
# exist, the function body is checked against the
# provided hints.
def invert(value: Union[int, str]) -> Union[int, str]:
if isinstance(value, str):
return int(value)
elif isinstance(value, int):
return str(value)
else:
raise ValueError("value must be 'int' or 'str'")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With