rspec/rules/S6542/python/rule.adoc

39 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
The type hint `Any` represents any possible Python type, on which all operations are possible. This conveys the information that all operations could be possible on a value annotated with `Any`, making the value dynamically-typed.
`Any` as a type hint provides no information about the expected type of the variable or parameter, essentially losing the benefits of type hinting. This can make the code less clear and harder to understand and will reduce the code insight capabilities of IDEs and static analysis tools.
== How to fix it
Replace this use of `Any` with a more specific type hint.
=== Code examples
==== Noncompliant code example
[source,python]
----
def foo(arg: Any) -> Any:
if isinstance(arg, int):
return arg + 1
else:
return arg.upper()
----
==== Compliant solution
[source,python]
----
def foo(arg: int | str) -> int | str:
if isinstance(arg, int):
return arg + 1
else:
return arg.upper()
----
== Resources
=== Documentation
* https://docs.python.org/3/library/typing.html#the-any-type[Python3 documentation on Any]
* https://mypy.readthedocs.io/en/stable/kinds_of_types.html#the-any-type[Mypy documentation on the Any type]