How to properly function annotate / type hint a list of strings

In Python 3.9+, list (with a lowercase l) can be used in type annotations and your code should work as is. On older versions of Python you need to import typing.List and use it instead

from typing import List

to_addresses: List[str]

Note the capital L.

You might want to consider something more specific. Why is from_address a str, but to_addresses is a list[str]? Maybe a

import typing
Address = typing.NewType("Address")

would be helpful.


Python 3.4 doesn't specify a format for its function annotations, it merely provides a mechanism that allows you to use any expression as the annotation. How the annotations are interpreted is up to you and the libraries you use.

Python 3.5 standardizes the way function annotations are used for type hinting, as documented in PEP 484. To annotate a list of strings, you use List[str], where List is imported from the typing module. You can also use Sequence[str] if your function accepts any list-like sequence, or Iterable[str] for any iterable.

Starting with Python 3.9, you can use list[str] as a type annotation, which doesn't require importing anything.


This syntax is now valid in Python 3.9+:

In type annotations you can now use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict) from typing.

Prior to 3.9 though, you need to use the imported List or in Python 3.7+ you can add

from __future__ import annotations

at the top of your file, which allows using list[int] (for example). Note though, this import only affects annotations:

from __future__ import annotations only affects annotations -- just the things after the colon. It makes it so that annotations are never evaluated

It still doesn't allow list[int] in arbitrary contexts.

Tags:

Python