For a new Python project using the latest version of Python should I declare my types as upper or lower case
Question:
According to pep-0585 for the latest versions of Python, it appears we can use List
and list
interchangeably for type declarations. So which should I use?
Assume:
- no requirement for backward compatibility
- using the latest version of python
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?
Answers:
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:
- Prior to python 3.9 you had to use
typing.List
(for example)
- From python 3.9 onward you can use either
list
or 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.
According to pep-0585 for the latest versions of Python, it appears we can use List
and list
interchangeably for type declarations. So which should I use?
Assume:
- no requirement for backward compatibility
- using the latest version of python
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:
- Prior to python 3.9 you had to use
typing.List
(for example) - From python 3.9 onward you can use either
list
ortyping.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.