diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index 3572b1c07f..1c7f20da86 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -1,4 +1,4 @@ -import asyncio +import sys from copy import deepcopy from functools import wraps @@ -32,6 +32,13 @@ _DEFAULT_TRANSACTION_NAME = "generic FastAPI request" +# Vendored: https://github.com/Kludex/starlette/blob/0a29b5ccdcbd1285c75c4fdb5d62ae1d244a21b0/starlette/_utils.py#L11-L17 +if sys.version_info >= (3, 13): # pragma: no cover + from inspect import iscoroutinefunction +else: + from asyncio import iscoroutinefunction + + class FastApiIntegration(StarletteIntegration): identifier = "fastapi" @@ -74,7 +81,7 @@ def _sentry_get_request_handler(*args: "Any", **kwargs: "Any") -> "Any": if ( dependant and dependant.call is not None - and not asyncio.iscoroutinefunction(dependant.call) + and not iscoroutinefunction(dependant.call) ): old_call = dependant.call diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index dac9887e2f..6d28dea3d0 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -1,6 +1,6 @@ -import asyncio import functools import warnings +import sys from collections.abc import Set from copy import deepcopy from json import JSONDecodeError @@ -74,6 +74,13 @@ multipart = None +# Vendored: https://github.com/Kludex/starlette/blob/0a29b5ccdcbd1285c75c4fdb5d62ae1d244a21b0/starlette/_utils.py#L11-L17 +if sys.version_info >= (3, 13): # pragma: no cover + from inspect import iscoroutinefunction +else: + from asyncio import iscoroutinefunction + + _DEFAULT_TRANSACTION_NAME = "generic Starlette request" TRANSACTION_STYLE_VALUES = ("endpoint", "url") @@ -424,8 +431,8 @@ def _is_async_callable(obj: "Any") -> bool: while isinstance(obj, functools.partial): obj = obj.func - return asyncio.iscoroutinefunction(obj) or ( - callable(obj) and asyncio.iscoroutinefunction(obj.__call__) + return iscoroutinefunction(obj) or ( + callable(obj) and iscoroutinefunction(obj.__call__) )