2023-08-04 16:46:24 +02:00
This rule raises an issue when an identity comparison operator is used to compare objects of different types.
2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-08-04 16:46:24 +02:00
Operators https://docs.python.org/3/reference/expressions.html#is-not[``++is++``] and https://docs.python.org/3/reference/expressions.html#is-not[``++is not++``] check if their operands point to the same instance, thus they will always return respectively ``++False++`` and ``++True++`` when they are used to compare objects of different types.
=== Code examples
2020-06-30 12:48:39 +02:00
2023-08-04 16:46:24 +02:00
==== Noncompliant code example
2020-06-30 12:48:39 +02:00
2023-08-04 16:46:24 +02:00
[source,python,diff-id=1,diff-type=noncompliant]
----
a = 1
b = "1"
value = a is b # Noncompliant. Always False
value = a is not b # Noncompliant. Always True
2020-06-30 12:48:39 +02:00
----
2023-08-04 16:46:24 +02:00
==== Compliant solution
[source,python,diff-id=1,diff-type=compliant]
2020-06-30 12:48:39 +02:00
----
2023-08-04 16:46:24 +02:00
a = 1
b = 1
value = a is b
value = a is not b
----
== Resources
=== Documentation
* Python documentation - https://docs.python.org/3/reference/expressions.html#is-not[Identity comparisons]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Remove this ["is"|"is not"] check between types X and Y; it will always be [False|True].
2021-09-20 15:38:42 +02:00
include::../highlighting.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]