rspec/rules/S5727/python/rule.adoc

67 lines
1.6 KiB
Plaintext
Raw Normal View History

2023-08-03 09:44:48 +02:00
This rule raises an issue when a comparison to `None` is invariant.
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
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 16:49:39 +02:00
:link-with-uscores1: https://docs.python.org/3/reference/datamodel.html#object.__eq__
== 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]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== 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
'''
== Comments And Links
(visible only on this page)
=== is related to: S5914
endif::env-github,rspecator-view[]