Skip to content

Commit 0b84014

Browse files
committed
change2
1 parent cb84e92 commit 0b84014

File tree

7 files changed

+40
-29
lines changed

7 files changed

+40
-29
lines changed

sentry_sdk/_compat.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import asyncio
33
import inspect
44

5-
from typing import TYPE_CHECKING
5+
from typing import TYPE_CHECKING, Any, Callable
66

77
if TYPE_CHECKING:
88
from typing import Any
@@ -12,6 +12,10 @@
1212
T = TypeVar("T")
1313
_F = TypeVar("_F", bound=Callable[..., Any])
1414

15+
# Public shim symbols with precise types so mypy accepts branch assignments
16+
iscoroutinefunction: "Callable[[Any], bool]"
17+
markcoroutinefunction: "Callable[[ _F ], _F]"
18+
1519

1620
PY37 = sys.version_info[0] == 3 and sys.version_info[1] >= 7
1721
PY38 = sys.version_info[0] == 3 and sys.version_info[1] >= 8
@@ -28,11 +32,26 @@
2832
iscoroutinefunction = inspect.iscoroutinefunction
2933
markcoroutinefunction = inspect.markcoroutinefunction
3034
else:
31-
iscoroutinefunction = asyncio.iscoroutinefunction # type: ignore[assignment]
35+
iscoroutinefunction = asyncio.iscoroutinefunction
3236

3337
def markcoroutinefunction(func):
3438
# type: (_F) -> _F
35-
func._is_coroutine = asyncio.coroutines._is_coroutine # type: ignore
39+
# Prior to Python 3.12, asyncio exposed a private `_is_coroutine`
40+
# marker used by asyncio.iscoroutinefunction(). This attribute was
41+
# removed in Python 3.11. If it's not available, fall back to a no-op,
42+
# which preserves behavior of inspect.iscoroutinefunction for our
43+
# supported versions while avoiding AttributeError.
44+
try:
45+
marker = getattr(asyncio.coroutines, "_is_coroutine")
46+
except Exception:
47+
# No marker available on this Python version; return function as-is.
48+
return func
49+
50+
try: # pragma: no cover - defensive
51+
func._is_coroutine = marker # type: ignore[attr-defined]
52+
except Exception:
53+
# If assignment fails for any reason, leave func unchanged.
54+
pass
3655
return func
3756

3857

sentry_sdk/ai/monitoring.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import inspect
22
from functools import wraps
33

4-
from sentry_sdk._compat import iscoroutinefunction
54
from sentry_sdk.consts import SPANDATA
65
import sentry_sdk.utils
76
from sentry_sdk import start_span
@@ -90,7 +89,7 @@ async def async_wrapped(*args, **kwargs):
9089
_ai_pipeline_name.set(None)
9190
return res
9291

93-
if iscoroutinefunction(f):
92+
if inspect.iscoroutinefunction(f):
9493
return wraps(f)(async_wrapped) # type: ignore
9594
else:
9695
return wraps(f)(sync_wrapped) # type: ignore

sentry_sdk/integrations/django/asgi.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
_F = TypeVar("_F", bound=Callable[..., Any])
3737

3838

39-
40-
41-
4239
def _make_asgi_request_event_processor(request):
4340
# type: (ASGIRequest) -> EventProcessor
4441
def asgi_request_event_processor(event, hint):

sentry_sdk/integrations/google_genai/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
)
1616

1717
import sentry_sdk
18-
from sentry_sdk._compat import iscoroutinefunction
1918
from sentry_sdk.ai.utils import set_data_normalized
2019
from sentry_sdk.consts import OP, SPANDATA
2120
from sentry_sdk.scope import should_send_default_pii
@@ -319,7 +318,7 @@ def wrapped_tool(tool):
319318
tool_name = getattr(tool, "__name__", "unknown")
320319
tool_doc = tool.__doc__
321320

322-
if iscoroutinefunction(tool):
321+
if inspect.iscoroutinefunction(tool):
323322
# Async function
324323
@wraps(tool)
325324
async def async_wrapped(*args, **kwargs):

sentry_sdk/integrations/quart.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ def _sentry_route(*args, **kwargs):
114114
def decorator(old_func):
115115
# type: (Any) -> Any
116116

117-
if inspect.isfunction(old_func) and not iscoroutinefunction(
118-
old_func
119-
):
117+
if inspect.isfunction(old_func) and not iscoroutinefunction(old_func):
120118

121119
@wraps(old_func)
122120
@ensure_integration_enabled(QuartIntegration, old_func)

sentry_sdk/tracing_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import uuid
1212

1313
import sentry_sdk
14-
from sentry_sdk._compat import iscoroutinefunction
1514
from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS, SPANTEMPLATE
1615
from sentry_sdk.utils import (
1716
capture_internal_exceptions,
@@ -913,7 +912,7 @@ def sync_wrapper(*args, **kwargs):
913912
except Exception:
914913
pass
915914

916-
if iscoroutinefunction(f):
915+
if inspect.iscoroutinefunction(f):
917916
return async_wrapper
918917
else:
919918
return sync_wrapper

tests/integrations/httpx/test_httpx.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import datetime
33
import asyncio
4+
import inspect
45
from unittest import mock
56

67
import httpx
@@ -9,7 +10,6 @@
910

1011
import sentry_sdk
1112
from sentry_sdk import capture_message, start_transaction
12-
from sentry_sdk._compat import iscoroutinefunction
1313
from sentry_sdk.consts import MATCH_ALL, SPANDATA
1414
from sentry_sdk.integrations.httpx import HttpxIntegration
1515
from tests.conftest import ApproxDict
@@ -33,7 +33,7 @@ def before_breadcrumb(crumb, hint):
3333
with start_transaction():
3434
events = capture_events()
3535

36-
if iscoroutinefunction(httpx_client.get):
36+
if inspect.iscoroutinefunction(httpx_client.get):
3737
response = asyncio.get_event_loop().run_until_complete(
3838
httpx_client.get(url)
3939
)
@@ -87,7 +87,7 @@ def test_crumb_capture_client_error(
8787
with start_transaction():
8888
events = capture_events()
8989

90-
if iscoroutinefunction(httpx_client.get):
90+
if inspect.iscoroutinefunction(httpx_client.get):
9191
response = asyncio.get_event_loop().run_until_complete(
9292
httpx_client.get(url)
9393
)
@@ -138,7 +138,7 @@ def test_outgoing_trace_headers(sentry_init, httpx_client, httpx_mock):
138138
op="greeting.sniff",
139139
trace_id="01234567890123456789012345678901",
140140
) as transaction:
141-
if iscoroutinefunction(httpx_client.get):
141+
if inspect.iscoroutinefunction(httpx_client.get):
142142
response = asyncio.get_event_loop().run_until_complete(
143143
httpx_client.get(url)
144144
)
@@ -181,7 +181,7 @@ def test_outgoing_trace_headers_append_to_baggage(
181181
op="greeting.sniff",
182182
trace_id="01234567890123456789012345678901",
183183
) as transaction:
184-
if iscoroutinefunction(httpx_client.get):
184+
if inspect.iscoroutinefunction(httpx_client.get):
185185
response = asyncio.get_event_loop().run_until_complete(
186186
httpx_client.get(url, headers={"baGGage": "custom=data"})
187187
)
@@ -334,7 +334,7 @@ def test_option_trace_propagation_targets(
334334

335335
# Must be in a transaction to propagate headers
336336
with sentry_sdk.start_transaction():
337-
if iscoroutinefunction(httpx_client.get):
337+
if inspect.iscoroutinefunction(httpx_client.get):
338338
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
339339
else:
340340
httpx_client.get(url)
@@ -421,7 +421,7 @@ def test_request_source_disabled(
421421
url = "http://example.com/"
422422

423423
with start_transaction(name="test_transaction"):
424-
if iscoroutinefunction(httpx_client.get):
424+
if inspect.iscoroutinefunction(httpx_client.get):
425425
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
426426
else:
427427
httpx_client.get(url)
@@ -458,7 +458,7 @@ def test_request_source_enabled(sentry_init, capture_events, httpx_client, httpx
458458
url = "http://example.com/"
459459

460460
with start_transaction(name="test_transaction"):
461-
if iscoroutinefunction(httpx_client.get):
461+
if inspect.iscoroutinefunction(httpx_client.get):
462462
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
463463
else:
464464
httpx_client.get(url)
@@ -495,7 +495,7 @@ def test_request_source(sentry_init, capture_events, httpx_client, httpx_mock):
495495
url = "http://example.com/"
496496

497497
with start_transaction(name="test_transaction"):
498-
if iscoroutinefunction(httpx_client.get):
498+
if inspect.iscoroutinefunction(httpx_client.get):
499499
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
500500
else:
501501
httpx_client.get(url)
@@ -548,7 +548,7 @@ def test_request_source_with_module_in_search_path(
548548
url = "http://example.com/"
549549

550550
with start_transaction(name="test_transaction"):
551-
if iscoroutinefunction(httpx_client.get):
551+
if inspect.iscoroutinefunction(httpx_client.get):
552552
from httpx_helpers.helpers import async_get_request_with_client
553553

554554
asyncio.get_event_loop().run_until_complete(
@@ -579,7 +579,7 @@ def test_request_source_with_module_in_search_path(
579579
is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep
580580
assert is_relative_path
581581

582-
if iscoroutinefunction(httpx_client.get):
582+
if inspect.iscoroutinefunction(httpx_client.get):
583583
assert data.get(SPANDATA.CODE_FUNCTION) == "async_get_request_with_client"
584584
else:
585585
assert data.get(SPANDATA.CODE_FUNCTION) == "get_request_with_client"
@@ -619,7 +619,7 @@ def fake_start_span(*args, **kwargs):
619619
"sentry_sdk.integrations.httpx.start_span",
620620
fake_start_span,
621621
):
622-
if iscoroutinefunction(httpx_client.get):
622+
if inspect.iscoroutinefunction(httpx_client.get):
623623
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
624624
else:
625625
httpx_client.get(url)
@@ -671,7 +671,7 @@ def fake_start_span(*args, **kwargs):
671671
"sentry_sdk.integrations.httpx.start_span",
672672
fake_start_span,
673673
):
674-
if iscoroutinefunction(httpx_client.get):
674+
if inspect.iscoroutinefunction(httpx_client.get):
675675
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
676676
else:
677677
httpx_client.get(url)
@@ -721,7 +721,7 @@ def test_span_origin(sentry_init, capture_events, httpx_client, httpx_mock):
721721
url = "http://example.com/"
722722

723723
with start_transaction(name="test_transaction"):
724-
if iscoroutinefunction(httpx_client.get):
724+
if inspect.iscoroutinefunction(httpx_client.get):
725725
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
726726
else:
727727
httpx_client.get(url)

0 commit comments

Comments
 (0)