Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are array typehints declared in Python? [duplicate]

Given this function:

def array_returner():
    return ["hello", "there"]

How would I typehint that this function returns an array of strings, without importing List (and using the List[str] syntax)?

like image 203
Newbyte Avatar asked Jun 06 '26 07:06

Newbyte


2 Answers

Like this

from typing import List
def array_returner() -> List[str]:
    pass

Since python3.9

def array_returner() -> list[str]:
    pass
like image 125
azro Avatar answered Jun 07 '26 22:06

azro


This is what you're looking for:

from typing import List

def array_returner() -> List[str]:
    return ["hello", "there"]
like image 26
Ian Avatar answered Jun 07 '26 22:06

Ian