2023-08-03 09:44:48 +02:00
This rule raises an issue when a comparison to `None` is invariant.
2023-05-03 11:06:20 +02:00
2023-08-03 09:44:48 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
2023-08-03 09:44:48 +02:00
Checking if a variable or parameter is `None` should only be done when you expect that it can be `None`. Doing so when the variable is always `None` or never `None` is confusing at best. At worse, there is a bug and the variable is not updated properly.
2021-04-28 16:49:39 +02:00
2023-08-03 09:44:48 +02:00
This rule raises an issue when expressions `X is None`, `X is not None`, `X == None` or `X != None` are constant, i.e. `X` is always None or never None.
2021-04-28 16:49:39 +02:00
2023-08-03 09:44:48 +02:00
=== Code examples
2021-04-28 18:08:03 +02:00
2023-08-03 09:44:48 +02:00
==== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-08-03 09:44:48 +02:00
[source,python,diff-id=1,diff-type=noncompliant]
----
def foo():
my_var = None
if my_var == None: # Noncompliant: always True.
...
2021-04-28 16:49:39 +02:00
----
2023-08-03 09:44:48 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-08-03 09:44:48 +02:00
[source,python,diff-id=1,diff-type=compliant]
----
def foo(my_var):
if my_var == None:
...
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
:link-with-uscores1: https://docs.python.org/3/reference/datamodel.html#object.__eq__
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
2023-08-03 09:44:48 +02:00
=== Documentation
2021-04-28 16:49:39 +02:00
* Python documentation - https://docs.python.org/3/reference/expressions.html#is-not[Identity comparisons]
* Python documentation - {link-with-uscores1}[``++__eq__++`` operator]
2021-04-28 18:08:03 +02:00
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 identity check; it will always be True/False
* Remove this == comparison; it will always be True/False
* Remove this != comparison; it will always be True/False
=== Highlighting
the "is", "is not", "==" or "!=" operator
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== is related to: S5914
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]