org.sonar.l10n.py.rules.python.S6545.html Maven / Gradle / Ivy
Why is this an issue?
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.
How to fix it
Replace the generic type from the typing
module with its built-in counterpart:
Legacy type
Replacement
typing.List[int]
list[int]
typing.Dict[str, int]
dict[str, int]
typing.Set[str]
set[str]
typing.FrozenSet[str]
frozenset[str]
typing.Tuple[int, int]
tuple[int, int]
typing.Tuple[int, ...]
tuple[int, ...]
typing.Iterable[int]
collections.abc.Iterable[int]
typing.Sequence[bool]
collections.abc.Sequence[bool]
typing.Mapping[str, int]
collections.abc.Mapping[str, int]
typing.Type[T]
type[T]
Refer to PEP-585 in the Resources section for the full list of replacements.
Code examples
Noncompliant code example
import typing
def print_numbers(numbers: typing.List[int]) -> None:
for n in numbers:
print(n)
Compliant solution
def print_numbers(numbers: list[int]) -> None:
for n in numbers:
print(n)
Resources
Documentation