All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.py.rules.python.S5890.html Maven / Gradle / Ivy

There is a newer version: 4.23.0.17664
Show newest version

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:

  • Type-related bugs: Assigning a value of an incompatible type to a variable with a specific type hint may lead to unexpected behavior or errors at runtime.
  • Readability and maintainability: Type hints improve code readability by explicitly stating the intended types of variables and functions. When the assigned value type doesn’t match the hint, it can confuse other developers and make the code harder to maintain.

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





© 2015 - 2024 Weber Informatics LLC | Privacy Policy