Menu Close

How to specify multiple return types using type-hints with Python?

Sometimes, we want to specify multiple return types using type-hints with Python.

In this article, we’ll look at how to specify multiple return types using type-hints with Python.

How to specify multiple return types using type-hints with Python?

To specify multiple return types using type-hints with Python, we can use Union.

For instance, we write

from typing import Union


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

to create the foo function that takes the client_id string parameter and returns either a list or a boolean as specified by

Union[list, bool]

Conclusion

To specify multiple return types using type-hints with Python, we can use Union.

Posted in Python, Python Answers