Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify multiple return types using type-hints

I have a function in python that can either return a bool or a list. Is there a way to specify the return types using type hints?

For example, is this the correct way to do it?

def foo(id) -> list or bool:     ... 
like image 252
Yahya Uddin Avatar asked Nov 26 '15 18:11

Yahya Uddin


People also ask

Can you have multiple return types in Python?

Python functions can return multiple values. These values can be stored in variables directly.

What Python data structure allows for the return of multiple items from a function?

Return multiple values using commas In Python, you can return multiple values by simply return them separated by commas. In Python, comma-separated values are considered tuples without parentheses, except where required by syntax.

Do type hints do anything?

Type hints improve IDEs and linters. They make it much easier to statically reason about your code. Type hints help you build and maintain a cleaner architecture. The act of writing type hints forces you to think about the types in your program.

Are type hints Pythonic?

Python's type hints provide you with optional static typing to leverage the best of both static and dynamic typing. Besides the str type, you can use other built-in types such as int , float , bool , and bytes for type hintings. To check the syntax for type hints, you need to use a static type checker tool.


1 Answers

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: 
like image 109
Bhargav Rao Avatar answered Oct 23 '22 16:10

Bhargav Rao