Skip to content

Commit

Permalink
ref(types): Correct ExcInfo type
Browse files Browse the repository at this point in the history
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]`.
  • Loading branch information
szokeasaurusrex committed Jul 10, 2024
1 parent 9d97d93 commit 1f17f46
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 2 additions & 3 deletions sentry_sdk/integrations/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 1f17f46

Please sign in to comment.