Skip to content

Commit 196cc7a

Browse files
fix(starlette/fastapi): Use inspect.iscoroutinefunction when Starlette does
1 parent 1faec55 commit 196cc7a

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

sentry_sdk/integrations/fastapi.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import asyncio
1+
import sys
22
from copy import deepcopy
33
from functools import wraps
44

@@ -32,6 +32,13 @@
3232
_DEFAULT_TRANSACTION_NAME = "generic FastAPI request"
3333

3434

35+
# Vendored: https://github.com/Kludex/starlette/blob/0a29b5ccdcbd1285c75c4fdb5d62ae1d244a21b0/starlette/_utils.py#L11-L17
36+
if sys.version_info >= (3, 13): # pragma: no cover
37+
from inspect import iscoroutinefunction
38+
else:
39+
from asyncio import iscoroutinefunction
40+
41+
3542
class FastApiIntegration(StarletteIntegration):
3643
identifier = "fastapi"
3744

@@ -74,7 +81,7 @@ def _sentry_get_request_handler(*args: "Any", **kwargs: "Any") -> "Any":
7481
if (
7582
dependant
7683
and dependant.call is not None
77-
and not asyncio.iscoroutinefunction(dependant.call)
84+
and not iscoroutinefunction(dependant.call)
7885
):
7986
old_call = dependant.call
8087

sentry_sdk/integrations/starlette.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import asyncio
21
import functools
32
import warnings
3+
import sys
44
from collections.abc import Set
55
from copy import deepcopy
66
from json import JSONDecodeError
@@ -74,6 +74,13 @@
7474
multipart = None
7575

7676

77+
# Vendored: https://github.com/Kludex/starlette/blob/0a29b5ccdcbd1285c75c4fdb5d62ae1d244a21b0/starlette/_utils.py#L11-L17
78+
if sys.version_info >= (3, 13): # pragma: no cover
79+
from inspect import iscoroutinefunction
80+
else:
81+
from asyncio import iscoroutinefunction
82+
83+
7784
_DEFAULT_TRANSACTION_NAME = "generic Starlette request"
7885

7986
TRANSACTION_STYLE_VALUES = ("endpoint", "url")
@@ -424,8 +431,8 @@ def _is_async_callable(obj: "Any") -> bool:
424431
while isinstance(obj, functools.partial):
425432
obj = obj.func
426433

427-
return asyncio.iscoroutinefunction(obj) or (
428-
callable(obj) and asyncio.iscoroutinefunction(obj.__call__)
434+
return iscoroutinefunction(obj) or (
435+
callable(obj) and iscoroutinefunction(obj.__call__)
429436
)
430437

431438

0 commit comments

Comments
 (0)