[Django] #30041: Can't use __init_subclass__ in the Model subclass directly (not as a mixin class)

18 views
Skip to first unread message

Django

unread,
Dec 13, 2018, 2:01:00 PM12/13/18
to django-...@googlegroups.com
#30041: Can't use __init_subclass__ in the Model subclass directly (not as a mixin
class)
-----------------------------------------+------------------------
Reporter: goosemania | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
There is [https://code.djangoproject.com/ticket/28695 a closed Django
ticket] which references PEP and describes the new {{{__init_subclass__}}}
in Python 3.6, it solves the case when {{{__init_subclass__}}} is used in
a mixin class.

Unfortunately, a direct usage of __init_subclass__ in a subclass of Django
Model class results in TypeError:

{{{
$ python manage.py shell
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/core/management/__init__.py", line 381, in
execute_from_command_line
utility.execute()
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in
import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in
_call_with_frames_removed
File "/home/tt/mysite/polls/models.py", line 25, in <module>
class MyPluginModel(MyMasterModel):
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/db/models/base.py", line 78, in __new__
new_class = super_new(cls, name, bases, new_attrs, **kwargs)
TypeError: __init_subclass__() missing 1 required positional argument:
'cls'
}}}


To reproduce, just use the code below in models.py:
(FWIW, I created a django project and then polls app using
[https://docs.djangoproject.com/en/2.1/intro/tutorial01/ the tutorial] and
then added the code to models.py)

{{{
from django.db import models


# It works as a mixin class.
# Expected/actual result: MyModel.some_attr == 'added_by
__init_subclass__'

class MyMixin:
def __init_subclass__(cls, **kwargs):
cls.some_attr = 'added_by __init_subclass__'
super().__init_subclass__(**kwargs)

class MyModel(models.Model, MyMixin):
pass


# It doesn't work directly in the subclass of the Model class
# Expected result: MyPluginModel.some_attr == 'added_by __init_subclass__'
# Actual result: TypeError: __init_subclass__() missing 1 required
positional argument: 'cls'

class MyMasterModel(models.Model): # remove inheritance from models.Model
and it will work.
def __init_subclass__(cls, **kwargs):
cls.some_attr = 'added_by __init_subclass__'
super().__init_subclass__(**kwargs)

class MyPluginModel(MyMasterModel):
pass

}}}

OS: Fedora 28
Django: 2.1.4
Python: 3.6.6

--
Ticket URL: <https://code.djangoproject.com/ticket/30041>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 13, 2018, 2:04:38 PM12/13/18
to django-...@googlegroups.com
#30041: Can't use __init_subclass__ in the Model subclass directly (not as a mixin
class)
-------------------------------------+-------------------------------------
Reporter: Tatiana | Owner: nobody
Tereshchenko |
Type: Bug | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tatiana Tereshchenko):

* type: Uncategorized => Bug


--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:1>

Django

unread,
Dec 13, 2018, 3:43:04 PM12/13/18
to django-...@googlegroups.com
#30041: Can't use __init_subclass__ in the Model subclass directly (not as a mixin
class)
-------------------------------------+-------------------------------------
Reporter: Tatiana | Owner: nobody
Tereshchenko |
Type: Bug | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tatiana Tereshchenko):

It seems that explicit {{{@classmethod}}} decorator helps, though
[https://www.python.org/dev/peps/pep-0487/#requiring-an-explicit-
decorator-on-init-subclass PEP says] that requiring it is among "rejected
design options".
See [https://www.python.org/dev/peps/pep-0487/#subclass-registration
example of __init_subclass__ usage from PEP]

--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:2>

Django

unread,
Dec 18, 2018, 10:35:27 AM12/18/18
to django-...@googlegroups.com
#30041: Can't use __init_subclass__ in the Model subclass directly (not as a mixin
class)
-------------------------------------+-------------------------------------
Reporter: Tatiana | Owner: nobody
Tereshchenko |
Type: Bug | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution:
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 felixxm):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/10763 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:3>

Django

unread,
Dec 22, 2018, 4:18:41 PM12/22/18
to django-...@googlegroups.com
#30041: Can't use __init_subclass__ in the Model subclass directly (not as a mixin
class)
-------------------------------------+-------------------------------------
Reporter: Tatiana | Owner: nobody
Tereshchenko |
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:4>

Django

unread,
Dec 22, 2018, 6:11:49 PM12/22/18
to django-...@googlegroups.com
#30041: Can't use __init_subclass__ in the Model subclass directly (not as a mixin
class)
-------------------------------------+-------------------------------------
Reporter: Tatiana | Owner: nobody
Tereshchenko |
Type: Bug | Status: closed

Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

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


Comment:

In [changeset:"a68ea231012434b522ce45c513d84add516afa60" a68ea23]:
{{{
#!CommitTicketReference repository=""
revision="a68ea231012434b522ce45c513d84add516afa60"
Fixed #29970, #30041 -- Made ModelBase.__new__() pass attrs without
contribute_to_class() to type.__new__().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:5>

Reply all
Reply to author
Forward
0 new messages