From 1f17f46472511a22365f8da020b9c0b3933d1286 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Wed, 10 Jul 2024 12:18:28 +0200 Subject: [PATCH] ref(types): Correct `ExcInfo` type Previously, we defined `ExcInfo` as `tuple[Type[BaseException] | None, BaseException | None, TracebackType | None]`, when in fact, the correct type is the narrower `tuple[Type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None]`. --- sentry_sdk/_types.py | 5 +++-- sentry_sdk/integrations/sanic.py | 5 ++--- sentry_sdk/utils.py | 9 ++++++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/_types.py b/sentry_sdk/_types.py index 14fa8d08c2..b82376e517 100644 --- a/sentry_sdk/_types.py +++ b/sentry_sdk/_types.py @@ -121,8 +121,9 @@ total=False, ) - ExcInfo = Tuple[ - Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType] + ExcInfo = Union[ + tuple[Type[BaseException], BaseException, Optional[TracebackType]], + tuple[None, None, None], ] Hint = Dict[str, Any] diff --git a/sentry_sdk/integrations/sanic.py b/sentry_sdk/integrations/sanic.py index f2f9b8168e..46250926ef 100644 --- a/sentry_sdk/integrations/sanic.py +++ b/sentry_sdk/integrations/sanic.py @@ -28,13 +28,12 @@ from typing import Callable from typing import Optional from typing import Union - from typing import Tuple from typing import Dict from sanic.request import Request, RequestParameters from sanic.response import BaseHTTPResponse - from sentry_sdk._types import Event, EventProcessor, Hint + from sentry_sdk._types import Event, EventProcessor, ExcInfo, Hint from sanic.router import Route try: @@ -325,7 +324,7 @@ def _legacy_router_get(self, *args): @ensure_integration_enabled(SanicIntegration) def _capture_exception(exception): - # type: (Union[Tuple[Optional[type], Optional[BaseException], Any], BaseException]) -> None + # type: (Union[ExcInfo, BaseException]) -> None with capture_internal_exceptions(): event, hint = event_from_exception( exception, diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index a84f2eb3de..935172333f 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1019,7 +1019,14 @@ def exc_info_from_error(error): else: raise ValueError("Expected Exception object to report, got %s!" % type(error)) - return exc_type, exc_value, tb + exc_info = (exc_type, exc_value, tb) + + if TYPE_CHECKING: + # This cast is safe because exc_type and exc_value are either both + # None or both not None. + exc_info = cast(ExcInfo, exc_info) + + return exc_info def event_from_exception(