Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

69 lines
2.2 KiB
Plaintext

== 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:
[frame=all]
[cols="^1,^1"]
|===
|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
[source,python]
----
import typing
def print_numbers(numbers: typing.List[int]) -> None:
for n in numbers:
print(n)
----
==== Compliant solution
[source,python]
----
def print_numbers(numbers: list[int]) -> None:
for n in numbers:
print(n)
----
== Resources
=== Documentation
- https://mypy.readthedocs.io/en/stable/builtin_types.html#generic-types[Mypy documentation on built-in generic types]
- https://peps.python.org/pep-0585/[PEP 585 - Type Hinting Generics In Standard Collections]
- https://docs.python.org/3/library/typing.html#generic-concrete-collections[Python documentation on generic collections]
ifdef::env-github,rspecator-view[]
== Implementation Specification
(visible only on this page)
=== Message
Use the built-in generic type `XXX` instead of its typing counterpart.
endif::env-github,rspecator-view[]