#36962: Missing call to superclass `__init__` during object initialization
------------------------------+--------------------------------------
     Reporter:  kochrac       |                    Owner:  (none)
         Type:  Bug           |                   Status:  closed
    Component:  Core (Other)  |                  Version:  6.0
     Severity:  Normal        |               Resolution:  fixed
     Keywords:                |             Triage Stage:  Unreviewed
    Has patch:  1             |      Needs documentation:  0
  Needs tests:  0             |  Patch needs improvement:  0
Easy pickings:  0             |                    UI/UX:  0
------------------------------+--------------------------------------
Changes (by kochrac):

 * resolution:   => fixed
 * status:  new => closed


Old description:

> [https://github.com/django/django/blob/main/django/contrib/admin/widgets.py#L94-L626
> / L94-L626]
> [https://github.com/django/django/blob/main/django/forms/widgets.py#L1116-L1116
> / L1116-L1116]
> [https://github.com/django/django/blob/main/django/contrib/gis/forms/widgets.py#L12-L12
> / L12-L12]
> [https://github.com/django/django/blob/main/django/db/models/expressions.py#L1880-L1880
> / L1880-L1880]
> [https://github.com/django/django/blob/main/django/core/handlers/wsgi.py#L56-L56
> / L56-L56]
> [https://github.com/django/django/blob/main/django/template/defaulttags.py#L909-L909
> / L909-L909]
> [https://github.com/django/django/blob/main/django/contrib/admin/widgets.py#L630-L630
> / L630-L630]
> [https://github.com/django/django/blob/main/django/contrib/gis/db/backends/oracle/adapter.py#L7-L7
> / L7-L7]
> [https://github.com/django/django/blob/main/django/db/backends/ddl_references.py#L72-L72
> / L72-L72]
> [https://github.com/django/django/blob/main/django/contrib/admin/widgets.py#L262-L262
> / 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 call `super()`.
> 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**
> [https://docs.python.org/3/reference/datamodel.html#object.__init__ /
> Python init]
> [https://docs.python.org/3/reference/datamodel.html#object.__init__ /
> Python Standard Library super]
> [https://docs.python.org/3/glossary.html#term-method-resolution-order /
> Python Glossary Method resolution order]

New description:

 
[https://github.com/django/django/blob/main/django/contrib/admin/widgets.py#L94-L626
 / L94-L626]
 [https://github.com/django/django/blob/main/django/forms/widgets.py#L1116-L1116
 / L1116-L1116]
 
[https://github.com/django/django/blob/main/django/contrib/gis/forms/widgets.py#L12-L12
 / L12-L12]
 
[https://github.com/django/django/blob/main/django/db/models/expressions.py#L1880-L1880
 / L1880-L1880]
 
[https://github.com/django/django/blob/main/django/core/handlers/wsgi.py#L56-L56
 / L56-L56]
 
[https://github.com/django/django/blob/main/django/template/defaulttags.py#L909-L909
 / L909-L909]
 
[https://github.com/django/django/blob/main/django/contrib/admin/widgets.py#L630-L630
 / L630-L630]
 
[https://github.com/django/django/blob/main/django/contrib/gis/db/backends/oracle/adapter.py#L7-L7
 / L7-L7]
 
[https://github.com/django/django/blob/main/django/db/backends/ddl_references.py#L72-L72
 / L72-L72]
 
[https://github.com/django/django/blob/main/django/contrib/admin/widgets.py#L262-L262
 / 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 call `super()`. 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**
 [https://docs.python.org/3/reference/datamodel.html#object.__init__ /
 Python init]
 [https://docs.python.org/3/reference/datamodel.html#object.__init__ /
 Python Standard Library super]
 [https://docs.python.org/3/glossary.html#term-method-resolution-order /
 Python Glossary Method resolution order]

--
Comment:

 Requested fix as https://github.com/django/django/pull/20804
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36962#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019ca6b4c36c-48da84b7-53fb-44be-9a9d-fefff1e22a91-000000%40eu-central-1.amazonses.com.

Reply via email to