This rule raises an issue when an assigned value type is not compatible with the type hint of the assigning variable.

Why is this an issue?

Type hints in Python allow you to specify the expected types of variables and function return values. While type hints are not enforced at runtime, they serve as documentation and can be checked using static type checkers to catch type-related errors during development.

When an assigned value type is incompatible with the type hint of the assigning variable, it can lead to several issues:

How to fix it

Assign the variable to the value compatible with the type hint or change the type hint to be compatible with the variable’s type.

Code examples

Noncompliant code example

def my_function():
    my_int: int = "string"  # Noncompliant

Compliant solution

def my_function():
    my_str: str = "string"

Resources

Documentation