ID: py/incomplete-ordering Kind: problem Severity: warning Precision: very-high Tags: - reliability - correctness Query suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
A class that implements an ordering operator (
__ge__) should implement all four in order that ordering between two objects is consistent and obeys the usual mathematical rules. If the ordering is inconsistent with default equality, then
__ne__ should also be implemented.
Ensure that all four ordering comparisons are implemented as well as
__ne__ if required.
It is not necessary to manually implement all four comparisons, the
functools.total_ordering class decorator can be used.
In this example only the
__lt__ operator has been implemented which could lead to inconsistent behavior.
__ge__, and in this case,
__ne__ should be implemented.
class IncompleteOrdering(object): def __init__(self, i): self.i = i def __lt__(self, other): return self.i < other.i
Python Language Reference: Rich comparisons in Python.