rspec/rules/S6543/python/rule.adoc

36 lines
1.2 KiB
Plaintext

== Why is this an issue?
Generic types, such as `list` or `dict` accept type arguments to specify the type of elements contained in the list or the keys and values in the dictionary.
If a generic type is used without a type argument, the type arguments will implicitly assumed to be `Any`. This makes the type hint less informative and makes the contract of the function or variable annotated with the type hint more difficult to understand.
Furthermore, incomplete type hints can hinder IDE autocompletion and code insight capabilities of static analyis tools.
== How to fix it
Add the missing type parameters to this generic type.
=== Code examples
==== Noncompliant code example
[source,python]
----
def print_list(numbers: list) -> None:
for n in numbers:
print(n)
----
==== Compliant solution
[source,python]
----
def print_list(numbers: list[int]) -> None:
for n in numbers:
print(n)
----
== Resources
=== Documentation
- https://docs.python.org/3/library/typing.html#generic-concrete-collections[Python documentation on generic collections]
- https://mypy.readthedocs.io/en/stable/builtin_types.html#generic-types[Built-in generic types]