From cd055175551883f00b63d7577bb36562e59488a2 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 4 Aug 2026 08:34:28 +0200 Subject: [PATCH 1/2] chore(django): Use iscoroutinefunction shim in patch_views --- sentry_sdk/integrations/django/views.py | 27 +++++-------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/sentry_sdk/integrations/django/views.py b/sentry_sdk/integrations/django/views.py index b24e4df45b..184f7071ea 100644 --- a/sentry_sdk/integrations/django/views.py +++ b/sentry_sdk/integrations/django/views.py @@ -9,17 +9,7 @@ if TYPE_CHECKING: from typing import Any - -try: - from asyncio import iscoroutinefunction -except ImportError: - iscoroutinefunction = None # type: ignore - - -try: - from sentry_sdk.integrations.django.asgi import wrap_async_view -except (ImportError, SyntaxError): - wrap_async_view = None # type: ignore +from sentry_sdk.integrations.django.asgi import iscoroutinefunction, wrap_async_view def patch_views() -> None: @@ -62,17 +52,10 @@ def sentry_patched_make_view_atomic( # efficient way to wrap views (or build a cache?) integration = sentry_sdk.get_client().get_integration(DjangoIntegration) - if integration is not None: - is_async_view = ( - iscoroutinefunction is not None - and wrap_async_view is not None - and iscoroutinefunction(callback) - ) - if is_async_view: - sentry_wrapped_callback = wrap_async_view(callback) - else: - sentry_wrapped_callback = _wrap_sync_view(callback) - + if integration is not None and iscoroutinefunction(callback): + sentry_wrapped_callback = wrap_async_view(callback) + elif integration is not None: + sentry_wrapped_callback = _wrap_sync_view(callback) else: sentry_wrapped_callback = callback From c18d8ca8e674d10e43e4a951515e2697581baa27 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 4 Aug 2026 09:35:26 +0200 Subject: [PATCH 2/2] use early returns --- sentry_sdk/integrations/django/views.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/django/views.py b/sentry_sdk/integrations/django/views.py index 184f7071ea..d37d54117d 100644 --- a/sentry_sdk/integrations/django/views.py +++ b/sentry_sdk/integrations/django/views.py @@ -52,14 +52,13 @@ def sentry_patched_make_view_atomic( # efficient way to wrap views (or build a cache?) integration = sentry_sdk.get_client().get_integration(DjangoIntegration) - if integration is not None and iscoroutinefunction(callback): - sentry_wrapped_callback = wrap_async_view(callback) - elif integration is not None: - sentry_wrapped_callback = _wrap_sync_view(callback) - else: - sentry_wrapped_callback = callback + if integration is None: + return callback + + if iscoroutinefunction(callback): + return wrap_async_view(callback) - return sentry_wrapped_callback + return _wrap_sync_view(callback) SimpleTemplateResponse.render = sentry_patched_render BaseHandler.make_view_atomic = sentry_patched_make_view_atomic