Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Inconsistent method resolution order

ID: py/inconsistent-mroKind: problemSecurity severity: Severity: errorPrecision: very-highTags:   - quality   - reliability   - correctnessQuery suites:   - 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 aTypeError will be raised at runtime.

Recommendation

Use a class hierarchy that is not ambiguous.

Example

The MRO of classX is justX,object. The program will fail when the MRO of classY needs to be calculated becauseobject precedesX in the definition ofY but the opposite is true in the MRO ofX.

classX(object):def__init__(self):print("X")classY(object,X):def__init__(self):print("Y")

References


[8]ページ先頭

©2009-2025 Movatter.jp