8

I'm porting a django application from 1.x to 2.1 and got stuck with the error that says "TypeError: object() takes no parameters". I'm trying to solve the problem for quite a while but not even got a clue even after days of debugging and searching online

Installed apps:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.github',
    'timezone_field',
    'axes',
    'humans',
    'boxes',
    'pages',
]

Middleware settings:

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
]

There are no problems with the indentation,

celery version : 4.2.1
raven version : 6.9.0
django version : 2.1

Here is my wsgi.py

import os
from raven.contrib.django.raven_compat.middleware.wsgi import Sentry

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
application=Sentry(get_wsgi_application())

Here is an excerpt from the error log

File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)   
File "<frozen importlib._bootstrap>", line 986, in _gcd_import   
File "<frozen importlib._bootstrap>", line 969, in _find_and_load   
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked  
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked   
File "<frozen importlib._bootstrap_external>", line 665, in exec_module   
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed   
File "/app/wsgi.py", line 16, in <module>
        application=Sentry(get_wsgi_application())   
File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
        return WSGIHandler()   
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 136, in __init__
        self.load_middleware()   
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 36, in load_middleware
        mw_instance = middleware(handler)
TypeError: object() takes no parameters

Error after using CustomSentry :

in <module>
    application = CustomSentry(get_wsgi_application())
  File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 136, in __init__
    self.load_middleware()
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 36, in load_middleware
    mw_instance = middleware(handler)
TypeError: object() takes no parameters

I tried to catch the exceptions using ExceptionMiddleware, now I'm getting the following error:

application = CustomSentry(get_wsgi_application())
File "/usr/local/lib/python3.5/dist-packages/django/utils/deprecation.py", line 85, in __init__
    super().__init__()
TypeError: __init__() missing 1 required positional argument: 'application'

Any help would be appreciated.

iamrameshkumar
  • 1,328
  • 1
  • 14
  • 34
  • 1
    can you add the file `/app/wsgi.py` ? – JPG Aug 31 '18 at 06:05
  • @JPG my wsgi contains nothing but the code mentioned in the question. – iamrameshkumar Aug 31 '18 at 06:50
  • Updated the thread with my middleware settings – iamrameshkumar Aug 31 '18 at 15:25
  • 1
    update the question with your `INSTALLED_APPS` please; and your `MIDDLEWARE` list is not in the right order. `'django.middleware.security.SecurityMiddleware',` should be _above_ `'whitenoise.middleware.WhiteNoiseMiddleware',` – Burhan Khalid Sep 02 '18 at 05:03
  • Included my INSTALLED_APPS settings in the post. There is no difference in the error log after moving up the django.middleware.security.SecurityMiddleware – iamrameshkumar Sep 02 '18 at 05:41
  • 1
    I would suggest adding a breakpoint (or print statement) before line 36 in `django/utils/deprecation.py` to inspect the variable `middleware_path`. – Daniel Hepper Sep 03 '18 at 12:42
  • 1
    Have you checked your versions of middlewares? It looks like whitenoise would be the culprit. Did you `pip install --upgrade` it to a version that is compatible with django 2.1? – Laurent S Sep 03 '18 at 13:10
  • @LaurentS Whitenoise version is 4.0 and is compatible with django 2.1 as per the documentation (http://whitenoise.evans.io/en/stable) – iamrameshkumar Sep 03 '18 at 16:54
  • The error persists even after commenting out whitenoise middleware in settings. – iamrameshkumar Sep 03 '18 at 17:17
  • 1
    As is mentioned above, I would try to figure out which middleware is causing the problem, either with a breakpoint, or by adding a `print` statement just above the line that causes problem. – Laurent S Sep 03 '18 at 17:36
  • https://stackoverflow.com/questions/49003357/type-error-in-get-wsgi-application-in-django-2-x-with-python3 this refers to a similar problem but there is no answer posted. – iamrameshkumar Sep 03 '18 at 17:39
  • No it's proper in the actual code. I think there is some formatting problem when I copy pasted the code here – iamrameshkumar Sep 05 '18 at 20:26

2 Answers2

9

The error indicates that you have an old-style middleware in your middleware list. Old-style middlewares did not receive an argument upon instantiation, while new-style middlewares receive a handler.

Now, according to your settings, the only non-Django middleware is whitenoise, but you say that the error persists even after commenting that out.

Here are some suggestion to help you figure out what's going on:

  • As I have commented, add a breakpoint or print statement to the Django source to figure out which middleware causes the issue.

  • Make sure that the settings file you are editing is the one that is actually used.

  • Use the Python shell to inspect the actual value of the MIDDLEWARE setting:

    $ python manage.py shell
    >>> from django.conf import settings
    >>> settings.MIDDLEWARE
    ...
    
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • inspecting settings doesn't give me any different result. I will try putting breakpoints in Django source and find out which middleware causes the problem and I think that is the only way to a solution here after. – iamrameshkumar Sep 06 '18 at 19:09
  • @Ram Where was the problem? – JPG Sep 07 '18 at 07:41
4

try this in you /app/wsgi.py module,

import os
from raven.contrib.django.raven_compat.middleware.wsgi import Sentry

from django.core.wsgi import get_wsgi_application
from django.utils.deprecation import MiddlewareMixin


class CustomSentry(MiddlewareMixin, Sentry):
    pass


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
application = CustomSentry(get_wsgi_application())



References
1. object() takes no parameters in django 1.10
2. Django exception middleware: TypeError: object() takes no parameters

JPG
  • 82,442
  • 19
  • 127
  • 206
  • Now I'm getting the same error starting at "application = CustomSentry(get_wsgi_application())" – iamrameshkumar Aug 31 '18 at 15:23
  • I guess it's something to deal with the get_wsgi_application(), you must be able to reproduce the error if you just create a wsgi.py in a project with the same code as above and try running it with the mentioned dependencies (celery 4.2.1, raven version : 6.9.0, django version : 2.1 & python 3.5) – iamrameshkumar Aug 31 '18 at 18:58
  • Related to the error "Since you are using the new MIDDLEWARE settings, your Middleware class must accept a get_response argument" as said in https://stackoverflow.com/questions/42232606/django-exception-middleware-typeerror-object-takes-no-parameters. If it is my own class I could have added get_response argument but the wsgi is actually a library and I don't have any control on it also it's in the latest version. – iamrameshkumar Aug 31 '18 at 19:05
  • Is it something related to the order of the MIDDLE_WARE settings?, I tried the possible combinations as much as I can. – iamrameshkumar Aug 31 '18 at 19:17
  • I couldn't reproduce the behaviour :( – JPG Sep 01 '18 at 04:21
  • @Ram Can you find out which middleware class is causing the problem by commenting out the middleware class from settings.py? – JPG Sep 02 '18 at 08:59
  • I'm trying various possible options. I will post it if I could find anything. – iamrameshkumar Sep 02 '18 at 09:51
  • 1
    Can you add the whitenoise version ? – JPG Sep 02 '18 at 09:57
  • whitenoise version is 4.0 – iamrameshkumar Sep 02 '18 at 11:21
  • 1
    Did you tried to run the server by commenting out whitenoise middleware ? – JPG Sep 02 '18 at 13:34
  • I tried to catch the exceptions by adding a ExceptionMiddleware, I'm getting the error "TypeError: __init__() missing 1 required positional argument: 'application'", Updated the same in the post. – iamrameshkumar Sep 02 '18 at 19:43
  • 1
    I reproduced the error by adding a custom middleware class as mentioned in my references-1. Is there any way you could share the project? – JPG Sep 03 '18 at 00:17
  • Sorry, I can't share the project. https://stackoverflow.com/questions/49003357/type-error-in-get-wsgi-application-in-django-2-x-with-python3 refers to a similar problem but there is no answer posted. – iamrameshkumar Sep 03 '18 at 17:43