How to specify multiple return types using type-hints

We Are Going To Discuss About How to specify multiple return types using type-hints. So lets Start this Python Article.

How to specify multiple return types using type-hints

How to solve How to specify multiple return types using type-hints

From the documentation
class typing.Union
Union type; Union[X, Y] means either X or Y.
Hence the proper way to represent more than one return data type is
from typing import Union def foo(client_id: str) -> Union[list,bool]
But do note that typing is not enforced. Python continues to remain a dynamically-typed language. The annotation syntax has been developed to help during the development of the code prior to being released into production. As PEP 484 states, “no type checking happens at runtime.”
>>> def foo(a:str) -> list: ... return("Works") ... >>> foo(1) 'Works'
As you can see I am passing a int value and returning a str. However the __annotations__ will be set to the respective values.
>>> foo.__annotations__ {'return': <class 'list'>, 'a': <class 'str'>}
Please Go through PEP 483 for more about Type hints. Also see What are type hints in Python 3.5??
Kindly note that this is available only for Python 3.5 and upwards. This is mentioned clearly in PEP 484.
From Python 3.10 onwards, there is a new way to represent this union. See Union Type:
A union object holds the value of the | (bitwise or) operation on multiple type objects. These types are intended primarily for type annotations. The union type expression enables cleaner type hinting syntax compared to typing.Union.
As we can see, this is exactly the same as typing.Union in the previous versions. Our previous example can be modified to use this notation:
def foo(client_id: str) -> list | bool:

How to specify multiple return types using type-hints

From the documentation
class typing.Union
Union type; Union[X, Y] means either X or Y.
Hence the proper way to represent more than one return data type is
from typing import Union def foo(client_id: str) -> Union[list,bool]
But do note that typing is not enforced. Python continues to remain a dynamically-typed language. The annotation syntax has been developed to help during the development of the code prior to being released into production. As PEP 484 states, “no type checking happens at runtime.”
>>> def foo(a:str) -> list: ... return("Works") ... >>> foo(1) 'Works'
As you can see I am passing a int value and returning a str. However the __annotations__ will be set to the respective values.
>>> foo.__annotations__ {'return': <class 'list'>, 'a': <class 'str'>}
Please Go through PEP 483 for more about Type hints. Also see What are type hints in Python 3.5??
Kindly note that this is available only for Python 3.5 and upwards. This is mentioned clearly in PEP 484.
From Python 3.10 onwards, there is a new way to represent this union. See Union Type:
A union object holds the value of the | (bitwise or) operation on multiple type objects. These types are intended primarily for type annotations. The union type expression enables cleaner type hinting syntax compared to typing.Union.
As we can see, this is exactly the same as typing.Union in the previous versions. Our previous example can be modified to use this notation:
def foo(client_id: str) -> list | bool:

Solution 1

From the documentation

class typing.Union

Union type; Union[X, Y] means either X or Y.

Hence the proper way to represent more than one return data type is

from typing import Union


def foo(client_id: str) -> Union[list,bool]

But do note that typing is not enforced. Python continues to remain a dynamically-typed language. The annotation syntax has been developed to help during the development of the code prior to being released into production. As PEP 484 states, “no type checking happens at runtime.”

>>> def foo(a:str) -> list:
...     return("Works")
... 
>>> foo(1)
'Works'

As you can see I am passing a int value and returning a str. However the __annotations__ will be set to the respective values.

>>> foo.__annotations__ 
{'return': <class 'list'>, 'a': <class 'str'>}

Please Go through PEP 483 for more about Type hints. Also see What are type hints in Python 3.5??

Kindly note that this is available only for Python 3.5 and upwards. This is mentioned clearly in PEP 484.


From Python 3.10 onwards, there is a new way to represent this union. See Union Type:

A union object holds the value of the | (bitwise or) operation on multiple type objects. These types are intended primarily for type annotations. The union type expression enables cleaner type hinting syntax compared to typing.Union.

As we can see, this is exactly the same as typing.Union in the previous versions. Our previous example can be modified to use this notation:

def foo(client_id: str) -> list | bool:

Original Author Bhargav Rao Of This Content

Solution 2

In case anyone landed here in search of “how to specify types of multiple return values?”, use Tuple[type_value1, ..., type_valueN]

from typing import Tuple

def f() -> Tuple[dict, str]:
    a = {1: 2}
    b = "hello"
    return a, b

More info: How to annotate types of multiple return values?

Original Author Anton Khodak Of This Content

Solution 3

The statement def foo(client_id: str) -> list or bool: when evaluated is equivalent to
def foo(client_id: str) -> list: and will therefore not do what you want.

The native way to describe a “either A or B” type hint is Union (thanks to Bhargav Rao):

def foo(client_id: str) -> Union[list, bool]:

Or, starting with Python 3.10 and beyond, using the | operator:

def foo(client_id: str) -> list | bool:

I do not want to be the “Why do you want to do this anyway” guy, but maybe having 2 return types isn’t what you want:

If you want to return a bool to indicate some type of special error-case, consider using Exceptions instead. If you want to return a bool as some special value, maybe an empty list would be a good representation.
You can also indicate that None could be returned with Optional[list]

Original Author Felk Of This Content

Solution 4

Python 3.10 (use |): Example for a function which takes a single argument that is either an int or str and returns either an int or str:

def func(arg: int | str) -> int | str:
              ^^^^^^^^^     ^^^^^^^^^ 
             type of arg   return type

Python 3.5 – 3.9 (use typing.Union):

from typing import Union
def func(arg: Union[int, str]) -> Union[int, str]:
              ^^^^^^^^^^^^^^^     ^^^^^^^^^^^^^^^ 
                type of arg         return type

For the special case of X | None you can use Optional[X].

Original Author xjcl Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment