pymd
pymd

Reputation: 4411

What are metaclass bases in Python?

I'm trying to extend django-allauth to do something specific to my projects. I'm basically trying to write my own wrapper on top of django-allauth, and want the installation, configuration and other stuff to be quite similar to allauth.

For this, I started with extending AppSettings class from allauth/accounts/app_settings.py. I created my own app_settings.py did something like this:

from allauth.account import app_settings as AllAuthAppSettings
class MyAppSettings (AllAuthAppSettings):
     def __init__(self, prefix):
         # do something

Also, at the end of the app_settings.py, I simply put the following (copying it from django-allauth itself):

import sys
my_app_settings = MyAppSettings('MY_PREFIX_')
my_app_settings.__name__ = __name__
sys.modules[__name__] = my_app_settings

Now, when I start my project, it gives me the following error:

TypeError: Error when calling the metaclass bases
    __init__() takes exactly 2 arguments (4 given)

Honestly, I'm quite new to the Python-Django world and don't really understand what's happening in those last four lines.

What is metaclass bases? What are the four arguments that are being passed to it? How do I make this flow work?

Here's the stack trace:

Unhandled exception in thread started by <function wrapper at 0x104146578>
Traceback (most recent call last):
  File "/Users/user/anaconda/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/user/anaconda/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/Users/user/anaconda/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/user/anaconda/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/user/anaconda/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/user/anaconda/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/user/anaconda/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/user/anaconda/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/user/myproject/my_app/models.py", line 18, in <module>
    from .model_managers import *
  File "/Users/user/myproject/my_app/model_managers.py", line 89, in <module>
    from . import app_settings
  File "/Users/user/myproject/my_app/app_settings.py", line 9, in <module>
TypeError: Error when calling the metaclass bases
    __init__() takes exactly 2 arguments (4 given)

Upvotes: 1

Views: 241

Answers (1)

Brendan Abel
Brendan Abel

Reputation: 37549

It doesn't look like you should be able to inherit from AllAuthAppSettings

The django-allauth package is doing some very ugly python magic

import sys  # noqa
app_settings = AppSettings('ACCOUNT_')
app_settings.__name__ = __name__
sys.modules[__name__] = app_settings

Basically, when you import the app_settings module, it creates an instance of the AppSettings class, renames it to the name of the app_settings module, and then replaces the imported module with the instance of the class!

You cannot inherit from class instances. I'm guessing you want to inherit from then non-instantiated AppSettings class. To do that, you need to inherit from the class of app_settings, not app_settings directly

from allauth.account import app_settings as AllAuthAppSettings


class MyAppSettings(AllAuthAppSettings.__class__):
    ...

I don't think you should need to copy those lines from the end of the app_settings module to hack your module into a class.

Upvotes: 1

Related Questions