Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return type annotation for Tuple in Python

I have function:

def func() -> tuple(str, list(str)):
    var_a = "four"
    var_b = ["one","two","three"]
    return var_a, var_b

And when I call it, it gives me the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not iterable

I have also tried it this way:

from typing import Tuple
def func() -> Tuple[str, list(str)]:
    var_a = "four"
    var_b = ["one","two","three"]
    return var_a, var_b

And, I am met with the exact same error.

How to have the proper return type annotation for this case?

like image 736
raghavsikaria Avatar asked Mar 24 '26 13:03

raghavsikaria


1 Answers

By using List[str], the generic version of list:

from typing import List, Tuple

def func() -> Tuple[str, List[str]]:
    var_a = "four"
    var_b = ["one","two","three"]
    return var_a, var_b
like image 182
Brad Solomon Avatar answered Mar 27 '26 09:03

Brad Solomon