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)