rspec/rules/S5727/python/rule.adoc

64 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +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.
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.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
mynone = None
result = mynone is None: # Noncompliant. Always True.
if mynone == None: # Noncompliant. Always True.
pass
if mynone is not None: # Noncompliant. Always False.
pass
if mynone == None: # Noncompliant. Always False.
pass
myint = 42
result = myint is None: # Noncompliant. Always False.
if myint == None: # Noncompliant. Always False.
pass
if myint is not None: # Noncompliant. Always True.
pass
if myint == None: # Noncompliant. Always True.
pass
----
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
* 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)
include::message.adoc[]
include::highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]