org.sonar.l10n.py.rules.python.S5727.html Maven / Gradle / Ivy
This rule raises an issue when a comparison to None
is invariant.
Why is this an issue?
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.
Code examples
Noncompliant code example
def foo():
my_var = None
if my_var == None: # Noncompliant: always True.
...
Compliant solution
def foo(my_var):
if my_var == None:
...
Resources
Documentation
- Python documentation - Identity comparisons
- Python documentation -
__eq__
operator
© 2015 - 2024 Weber Informatics LLC | Privacy Policy