Skip to content

Commit 6d83d5e

Browse files
move _markcoroutinefunction back to django asgi
1 parent e8f494d commit 6d83d5e

2 files changed

Lines changed: 23 additions & 20 deletions

File tree

sentry_sdk/integrations/_asgi_common.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,19 @@
1111
from typing import Any
1212
from typing import Dict
1313
from typing import Optional
14-
from typing import TypeVar
1514
from typing import Union
1615
from typing_extensions import Literal
1716

1817
from sentry_sdk.utils import AnnotatedValue
1918

20-
_F = TypeVar("_F")
21-
22-
2319
# Python 3.12 deprecates asyncio.iscoroutinefunction() as an alias for
24-
# inspect.iscoroutinefunction(), whilst also removing the _is_coroutine marker.
25-
# The latter is replaced with inspect.markcoroutinefunction().
26-
# Until 3.12 is the minimum supported Python version, provide a shim.
27-
# This was copied from https://github.com/django/asgiref/blob/main/asgiref/sync.py
28-
if hasattr(inspect, "markcoroutinefunction"):
20+
# inspect.iscoroutinefunction(). Until 3.12 is the minimum supported Python
21+
# version, provide a shim.
22+
if hasattr(inspect, "iscoroutinefunction"):
2923
_iscoroutinefunction = inspect.iscoroutinefunction
30-
_markcoroutinefunction = inspect.markcoroutinefunction
3124
else:
3225
_iscoroutinefunction = asyncio.iscoroutinefunction # type: ignore[assignment]
3326

34-
def _markcoroutinefunction(func: "_F") -> "_F":
35-
func._is_coroutine = asyncio.coroutines._is_coroutine # type: ignore[attr-defined]
36-
return func
37-
3827

3928
def _get_headers(asgi_scope: "Any") -> "Dict[str, str]":
4029
"""

sentry_sdk/integrations/django/asgi.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
`django.core.handlers.asgi`.
77
"""
88

9+
import asyncio
910
import functools
11+
import inspect
1012

1113
from django.core.handlers.wsgi import WSGIRequest
1214

1315
import sentry_sdk
1416
from sentry_sdk.consts import OP
1517

1618
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
17-
from sentry_sdk.integrations._asgi_common import (
18-
_iscoroutinefunction,
19-
_markcoroutinefunction,
20-
)
19+
from sentry_sdk.integrations._asgi_common import _iscoroutinefunction
2120
from sentry_sdk.scope import should_send_default_pii
2221
from sentry_sdk.utils import (
2322
capture_internal_exceptions,
@@ -27,13 +26,28 @@
2726
from typing import TYPE_CHECKING
2827

2928
if TYPE_CHECKING:
30-
from typing import Any, Callable, Union
29+
from typing import Any, Callable, Union, TypeVar
3130

3231
from django.core.handlers.asgi import ASGIRequest
3332
from django.http.response import HttpResponse
3433

3534
from sentry_sdk._types import Event, EventProcessor
3635

36+
_F = TypeVar("_F", bound=Callable[..., Any])
37+
38+
# Python 3.12 deprecates asyncio.iscoroutinefunction() as an alias for
39+
# inspect.iscoroutinefunction(), whilst also removing the _is_coroutine marker.
40+
# The latter is replaced with the inspect.markcoroutinefunction decorator.
41+
# Until 3.12 is the minimum supported Python version, provide a shim.
42+
# This was copied from https://github.com/django/asgiref/blob/main/asgiref/sync.py
43+
if hasattr(inspect, "markcoroutinefunction"):
44+
markcoroutinefunction = inspect.markcoroutinefunction
45+
else:
46+
47+
def markcoroutinefunction(func: "_F") -> "_F":
48+
func._is_coroutine = asyncio.coroutines._is_coroutine # type: ignore
49+
return func
50+
3751

3852
def _make_asgi_request_event_processor(request: "ASGIRequest") -> "EventProcessor":
3953
def asgi_request_event_processor(event: "Event", hint: "dict[str, Any]") -> "Event":
@@ -200,7 +214,7 @@ def _async_check(self) -> None:
200214
Taken from django.utils.deprecation::MiddlewareMixin._async_check
201215
"""
202216
if _iscoroutinefunction(self.get_response):
203-
_markcoroutinefunction(self)
217+
markcoroutinefunction(self)
204218

205219
def async_route_check(self) -> bool:
206220
"""

0 commit comments

Comments
 (0)