Skip to content

Commit

Permalink
fix(django): SentryWrappingMiddleware.__init__ fails if super() is ob…
Browse files Browse the repository at this point in the history
…ject

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 <daniel.szoke@sentry.io>
  • Loading branch information
cameron-simpson and szokeasaurusrex committed Sep 4, 2024
1 parent 9df2b21 commit 16d05f4
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions sentry_sdk/integrations/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 16d05f4

Please sign in to comment.