According to pep-0585 for the latest versions of Python, it appears we can use
list interchangeably for type declarations. So which should I use?
from typing import List def hello_world_1(animals: list[str]) -> list[str]: return animals def hello_world_2(animals: List[str]) -> List[str]: return animals
How can I set up a python linter to enforce consistency and only allow either upper or lowercase
List for example?
In general … it is your choice. AFAIK, there is no PEP recommendation on which one to use.
If you have decided to use generic type hinting:
typing.List. Again … your choice.
(If you are asking for our recommendations, that is explicitly off-topic for StackOverflow! Maybe discuss it with your co-workers, other people working on your projects, …)
PEP585 (linked in the OP) described the imports from
typing as cumbersome and confusing. This seems to answer the question of which style is preferred.
According to the current Python docs,
typing.List and similar are deprecated.
The docs further state
The deprecated types will be removed from the typing module in the first Python version released 5 years after the release of Python 3.9.0. See details in PEP 585—Type Hinting Generics In Standard Collections.
Concerning enforcing consistency, it says
It is expected that type checkers will flag the deprecated types when the checked program targets Python 3.9 or newer.
However, I use Pylint personally (not a type checker, admittedly) and I don’t believe I’ve noticed it flagging the deprecated types yet.