8

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)?

8

2 Answers 2

14

Like this

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

Since python3.9

def array_returner() -> list[str]:
    pass
Sign up to request clarification or add additional context in comments.

Comments

12

This is what you're looking for:

from typing import List

def array_returner() -> List[str]:
    return ["hello", "there"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.