Opened 6 hours ago
Closed 6 hours ago
#36962 closed Bug (fixed)
Missing call to superclass `__init__` during object initialization
| Reported by: | kochengrac | Owned by: | |
|---|---|---|---|
| Component: | Core (Other) | Version: | 6.0 |
| Severity: | Normal | Keywords: | |
| Cc: | kochengrac | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
/ L94-L626
/ L1116-L1116
/ L12-L12
/ L1880-L1880
/ L56-L56
/ L909-L909
/ L630-L630
/ L7-L7
/ L72-L72
/ L262-L262
Missing call to superclass init during object initialization
Pythonunlike some other object-oriented languages such as Java, allows the developer complete freedom in when and how superclass initializers are called during object initialization. However, the developer has responsibility for ensuring that objects are properly initialized, and that all superclass __init__ methods are called.
If the __init__ method of a superclass is not called during object initialization, this can lead to errors due to the object not being fully initialized, such as having missing attributes. A call to the __init__ method of a superclass during object initialization may be unintentionally skipped:
- If a subclass calls the
__init__method of the wrong class. - If a call to the
__init__method of one its base classes is omitted. - If a call to
super().__init__is used, but not all__init__methods in the Method Resolution Order (MRO) chain themselves callsuper(). This in particular arises more often in cases of multiple inheritance.
explicit calls to init are used, but SportsCar erroneously calls Vehicle.init. This is fixed in FixedSportsCar by calling Car.init.
class Vehicle(object):
def __init__(self):
self.mobile = True
class Car(Vehicle):
def __init__(self):
Vehicle.__init__(self)
self.car_init()
class SportsCar(Car, Vehicle):
def __init__(self):
Vehicle.__init__(self)
self.sports_car_init()
class FixedSportsCar(Car, Vehicle):
def __init__(self):
Car.__init__(self)
self.sports_car_init()
References
/ Python init
/ Python Standard Library super
/ Python Glossary Method resolution order
Change History (1)
comment:1 by , 6 hours ago
| Description: | modified (diff) |
|---|---|
| Resolution: | → fixed |
| Status: | new → closed |
Requested fix as https://github.com/django/django/pull/20804