Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For a new Python project using the latest version of Python should I declare my types as upper or lower case

Tags:

python

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:

  1. no requirement for backward compatibility
  2. 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?

like image 300
MattG Avatar asked Dec 01 '25 10:12

MattG


1 Answers

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.

like image 59
msailor Avatar answered Dec 03 '25 03:12

msailor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!