Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make a type hint for multiple returns

I want to specify my function with correct type hints for return values. My function receives data as a dictionary and returns the modification. It also returns a status code as integer to check if the modification was successfull. For example I want to specify something like this:

def reworkData(measurement:dict)->dict,int:
   ...
   return measurement,0

This currently results in invalid syntax. How can I do this?

like image 634
StefanMaier Avatar asked Nov 18 '25 01:11

StefanMaier


1 Answers

From Python 3.5 to 3.8 you want Tuple[dict, int] in order to specify that you will return both as Python will return them as a tuple.

from typing import Tuple

def rework_data(measurement: dict) -> Tuple[dict, int]:
   ...
   return measurement, 0

As of Python 3.9 Tuple is deprecated and you should use the builtin type

def rework_data(measurement: dict) -> tuple[dict, int]:
   ...
   return measurement, 0
like image 146
Kemp Avatar answered Nov 20 '25 14:11

Kemp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!