First argument to super() is not enclosing class¶
ID: py/super-not-enclosing-classKind: problemSecurity severity: Severity: errorPrecision: highTags: - quality - reliability - correctness - external/cwe/cwe-687Query suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
Thesuper class should be called with the enclosing class as its first argument andself as its second argument.
Passing a different class may work correctly, provided the class passed is a super class of the enclosing class and the enclosing class does not define an__init__ method. However, this may result in incorrect object initialization if the enclosing class is later subclassed using multiple inheritance.
Recommendation¶
Ensure that the first argument tosuper() is the enclosing class.
Example¶
In this example, the call tosuper(Vehicle,self) inCar.__init__ is incorrect, as it passesVehicle rather thanCar as the first argument tosuper. As a result,super(SportsCar,self).__init__() in theSportsCar.__init__ method will not call all__init__() methods because the call tosuper(Vehicle,self).__init__() skipsStatusSymbol.__init__().
classVehicle(object):passclassCar(Vehicle):def__init__(self):#This is OK provided that Car is not subclassed.super(Vehicle,self).__init__()self.car_init()classStatusSymbol(object):def__init__(self):super(StatusSymbol,self).__init__()self.show_off()classSportsCar(Car,StatusSymbol):def__init__(self):#This will not call StatusSymbol.__init__()super(SportsCar,self).__init__()self.sports_car_init()#Fix Car by passing Car to super().#SportsCar does not need to be changed.classCar(Car,Vehicle):def__init__(self):super(Car,self).__init__()self.car_init()
References¶
Python Standard Library:super.
Artima Developer:Things to Know About Python Super.
Common Weakness Enumeration:CWE-687.