org.sonar.l10n.py.rules.python.S5655.html Maven / Gradle / Ivy
This rule raises an issue when a function or method is called with an argument of a different type than the one described in its type
annotations.
Why is this an issue?
The CPython interpreter does not check types of arguments when functions are called. However, a function can express the type it expects for each
argument in its documentation or by using Type Hints. While the code may initially work as
intended, not respecting the contract of an API may lead to bugs later when its implementation evolves or when type checks are added (i.e. with
isinstance
).
This rule also checks argument types for built-in functions.
Noncompliant code example
def func(var: str):
pass
func(42) # Noncompliant: 42 is not of type str.
round("not a number") # Noncompliant: the builtin function round requires a number as first parameter.
Compliant solution
def func(var: str):
pass
func("42")
round(1.2)
Resources
Documentation
- Python documentation - builtins
- Python documentation - typing — Support for type hints
- PEP 484 - Type Hints
© 2015 - 2024 Weber Informatics LLC | Privacy Policy