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 — Inconsistent method resolution order — CodeQL query help documentation

Inconsistent method resolution order¶

ID: py/inconsistent-mro
Kind: problem
Security severity: 
Severity: error
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

Python 2.3 introduced new-style classes (classes inheriting from object). New-style classes use the C3 linearization method to determine a method resolution ordering (MRO) for each class. The C3 linearization method ensures that for a class C, if a class C1 precedes a class C2 in the MRO of C then C1 should also precede C2 in the MRO of all subclasses of C. It is possible to create a situation where it is impossible to achieve this consistency and this will guarantee that a TypeError will be raised at runtime.

Recommendation¶

Use a class hierarchy that is not ambiguous.

Example¶

The MRO of class X is just X, object. The program will fail when the MRO of class Y needs to be calculated because object precedes X in the definition of Y but the opposite is true in the MRO of X.

class X(object):
    def __init__(self):
        print("X")
class Y(object,X):
    def __init__(self):
        print("Y")

References¶

  • Python: The Python 2.3 Method Resolution Order.

  • © 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.