In some cases a comparison with operators ==, or != will always return True or always return False. When this happens,
the comparison and all its dependent code can simply be removed. This includes:
None with a value which will always be None. __eq__, or __ne__ if != is used, to an object of a
different type (builtin or from an unrelated class which also doesn't implement __eq__ or __ne__).
mynone = None
foo = mynone == None # Noncompliant. Always True.
foo = 1 == "1" # Noncompliant. Always False.
foo = 1 != "1" # Noncompliant. Always True.
class A:
pass
myvar = A() == 1 # Noncompliant. Always False.
myvar = A() != 1 # Noncompliant. Always True.
class Ne:
def __ne__(self, other):
return True
myvar = Ne() == 1 # Noncompliant. Always False. "__eq__" does not call "__ne__" by default
myvar = 1 == Ne() # Noncompliant. Always False.
myvar = Ne() != 1 # Ok
myvar = 1 != Ne() # Ok
foo = 1 == int("1")
foo = str(1) != "1"
class Eq:
def __eq__(self, other):
return True
myvar = Eq() == 1
myvar = 1 == Eq()
myvar = Eq() != 1 # Ok. "__ne__" calls "__eq__" by default
myvar = 1 != Eq()