GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Incomplete ordering — CodeQL query help documentation

Incomplete ordering¶

ID: py/incomplete-ordering
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - python-code-quality.qls
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

A class that implements the rich comparison operators (__lt__, __gt__, __le__, or __ge__) should ensure that all four comparison operations <, <=, >, and >= function as expected, consistent with expected mathematical rules. In Python 3, this is ensured by implementing one of __lt__ or __gt__, and one of __le__ or __ge__. If the ordering is not consistent with default equality, then __eq__ should also be implemented.

Recommendation¶

Ensure that at least one of __lt__ or __gt__ and at least one of __le__ or __ge__ is defined.

The functools.total_ordering class decorator can be used to automatically implement all four comparison methods from a single one, which is typically the cleanest way to ensure all necessary comparison methods are implemented consistently.

Example¶

In the following example, only the __lt__ operator has been implemented, which would lead to unexpected errors if the <= or >= operators are used on A instances. The __le__ method should also be defined, as well as __eq__ in this case.

class A:
    def __init__(self, i):
        self.i = i

    # BAD: le is not defined, so `A(1) <= A(2)` would result in an error.
    def __lt__(self, other):
        return self.i < other.i
    

References¶

  • Python Language Reference: Rich comparisons in Python.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.