Python 3.9 introduced built-in generic types such as list[T], dict[T], set[T] to make type hints more
concise and easier to read. These built-in types have the same functionality as their counterparts in the typing module, but are more
readable and idiomatic.
Using types such as typing.List is confusing in the presence of the already existing built-in types. This can also create
inconsistencies when different parts of the codebase use different syntaxes for the same type.
Replace the generic type from the typing module with its built-in counterpart:
Legacy type |
Replacement |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Refer to PEP-585 in the Resources section for the full list of replacements.
import typing
def print_numbers(numbers: typing.List[int]) -> None:
for n in numbers:
print(n)
def print_numbers(numbers: list[int]) -> None:
for n in numbers:
print(n)