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

[source,python,diff-id=1,diff-type=noncompliant]
----
def my_function():
    my_int: int = "string"  # Noncompliant
----


==== Compliant solution

[source,python,diff-id=1,diff-type=compliant]
----
def my_function():
    my_str: str = "string" 
----

== Resources

=== Documentation

* Python documentation - https://docs.python.org/3/library/typing.html[typing — Support for type hints]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

* If the asignment value is not ``++None++`` then 
** Primary: Assign to "XXX" a value of type "YYY" instead of "ZZZ" or update the type hint of "XXX".
** Secondary: Type hint.
* If the assignment value is ``++None++``
** Primary: Replace the type hint "XXX" with "Optional[XXX]" or don't assign "None" to "YYY"
** Secondary: Type hint.


=== Highlighting

* Primary: The assigned value
* Secondary: The type hint


'''
== Comments And Links
(visible only on this page)

=== on 12 May 2021, 00:45:18 Mike Heyns wrote:
This rule does not appear to be working for https://docs.python.org/3/library/dataclasses.html#init-only-variables[Dataclass Init-Only variables]:


----
@dataclass
class Book:
    name: str     
    condition: InitVar[str] = ''
----

____Assign to "condition" a value of type "InitVar[str]" instead of "str" or update its type hint.____


In fact, most examples including the official documentation use ``++None++`` as the default for init-only arguments. Attempting to use ``++InitVar[Optional[str]]++`` has the same effect as above.

endif::env-github,rspecator-view[]
