From 16d05f4e44d5f4c9082144f864784e63204a4bd9 Mon Sep 17 00:00:00 2001 From: Cameron Simpson Date: Wed, 4 Sep 2024 17:59:03 +1000 Subject: [PATCH] fix(django): SentryWrappingMiddleware.__init__ fails if super() is object As described in issue #2461, the SentryWrappingMiddleware MRO is just object if Django < 3.1 (when async middleware became a thing), but the async_capable check inside the class only looks for the async_capable attribute inside the middleware class. This PR makes that check also conditional on Django >= 3.1. Otherwise the code calls super(.....).__init__(get_response) and for Django < 3.1 this only finds object.__init__, not the wrapped middleware __init__. --- Co-authored-by: Daniel Szoke --- sentry_sdk/integrations/django/middleware.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/integrations/django/middleware.py b/sentry_sdk/integrations/django/middleware.py index 1abf6ec4e2..981d192864 100644 --- a/sentry_sdk/integrations/django/middleware.py +++ b/sentry_sdk/integrations/django/middleware.py @@ -30,7 +30,9 @@ "import_string_should_wrap_middleware" ) -if DJANGO_VERSION < (3, 1): +DJANGO_SUPPORTS_ASYNC_MIDDLEWARE = DJANGO_VERSION >= (3, 1) + +if not DJANGO_SUPPORTS_ASYNC_MIDDLEWARE: _asgi_middleware_mixin_factory = lambda _: object else: from .asgi import _asgi_middleware_mixin_factory @@ -123,7 +125,9 @@ def sentry_wrapped_method(*args, **kwargs): class SentryWrappingMiddleware( _asgi_middleware_mixin_factory(_check_middleware_span) # type: ignore ): - async_capable = getattr(middleware, "async_capable", False) + async_capable = DJANGO_SUPPORTS_ASYNC_MIDDLEWARE and getattr( + middleware, "async_capable", False + ) def __init__(self, get_response=None, *args, **kwargs): # type: (Optional[Callable[..., Any]], *Any, **Any) -> None