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 400a482dd3..5006e6323a 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -16,6 +16,7 @@ from decimal import Decimal from functools import partial, partialmethod, wraps from numbers import Real +from typing import cast from urllib.parse import parse_qs, unquote, urlencode, urlsplit, urlunsplit try: @@ -28,6 +29,7 @@ import sentry_sdk import sentry_sdk.hub from sentry_sdk._compat import PY37 +from sentry_sdk._types import ExcInfo # This type must be available at runtime! from sentry_sdk._types import TYPE_CHECKING from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH, EndpointType @@ -38,7 +40,6 @@ from typing import ( Any, Callable, - cast, ContextManager, Dict, Iterator, @@ -57,7 +58,7 @@ from gevent.hub import Hub import sentry_sdk.integrations - from sentry_sdk._types import Event, ExcInfo + from sentry_sdk._types import Event P = ParamSpec("P") R = TypeVar("R") @@ -1012,7 +1013,10 @@ def exc_info_from_error(error): else: raise ValueError("Expected Exception object to report, got %s!" % type(error)) - return exc_type, exc_value, tb + # This cast is safe because exc_type and exc_value are either both + # None or both not None. + exc_info = cast(ExcInfo, (exc_type, exc_value, tb)) + return exc_info def event_from_exception(