From 5ce2e98b90cd1eaa696384a393d6735af94e85e9 Mon Sep 17 00:00:00 2001 From: Buck Evan Date: Mon, 9 Oct 2023 13:51:48 -0500 Subject: [PATCH] feat(typing): annotate Event more uniformly This helped me (and my IDE) understand the code more fully. --- sentry_sdk/crons/api.py | 4 +++- sentry_sdk/envelope.py | 10 ++++++---- sentry_sdk/integrations/chalice.py | 4 ++-- sentry_sdk/integrations/spark/spark_worker.py | 2 +- sentry_sdk/session.py | 4 ++-- sentry_sdk/sessions.py | 4 +++- sentry_sdk/utils.py | 9 +++++++-- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/sentry_sdk/crons/api.py b/sentry_sdk/crons/api.py index cd240a7dcd..025320d576 100644 --- a/sentry_sdk/crons/api.py +++ b/sentry_sdk/crons/api.py @@ -7,6 +7,8 @@ if TYPE_CHECKING: from typing import Any, Dict, Optional + from sentry_sdk._types import Event + def _create_check_in_event( monitor_slug=None, @@ -15,7 +17,7 @@ def _create_check_in_event( duration_s=None, monitor_config=None, ): - # type: (Optional[str], Optional[str], Optional[str], Optional[float], Optional[Dict[str, Any]]) -> Dict[str, Any] + # type: (Optional[str], Optional[str], Optional[str], Optional[float], Optional[Dict[str, Any]]) -> Event options = Hub.current.client.options if Hub.current.client else {} check_in_id = check_in_id or uuid.uuid4().hex # type: str diff --git a/sentry_sdk/envelope.py b/sentry_sdk/envelope.py index a3e4b5a940..4b69c5ad41 100644 --- a/sentry_sdk/envelope.py +++ b/sentry_sdk/envelope.py @@ -79,11 +79,13 @@ def add_session( ): # type: (...) -> None if isinstance(session, Session): - session = session.to_json() - self.add_item(Item(payload=PayloadRef(json=session), type="session")) + json = session.to_json() + else: + json = session + self.add_item(Item(payload=PayloadRef(json=json), type="session")) def add_sessions( - self, sessions # type: Any + self, sessions # type: Event ): # type: (...) -> None self.add_item(Item(payload=PayloadRef(json=sessions), type="sessions")) @@ -160,7 +162,7 @@ def __init__( self, bytes=None, # type: Optional[bytes] path=None, # type: Optional[Union[bytes, text_type]] - json=None, # type: Optional[Any] + json=None, # type: Optional[Event] ): # type: (...) -> None self.json = json diff --git a/sentry_sdk/integrations/chalice.py b/sentry_sdk/integrations/chalice.py index 25d8b4ac52..e27ba3c9d9 100644 --- a/sentry_sdk/integrations/chalice.py +++ b/sentry_sdk/integrations/chalice.py @@ -47,12 +47,12 @@ def __call__(self, event, context): return ChaliceEventSourceHandler.__call__(self, event, context) except Exception: exc_info = sys.exc_info() - event, hint = event_from_exception( + sentry_event, hint = event_from_exception( exc_info, client_options=client.options, mechanism={"type": "chalice", "handled": False}, ) - hub.capture_event(event, hint=hint) + hub.capture_event(sentry_event, hint=hint) hub.flush() reraise(*exc_info) diff --git a/sentry_sdk/integrations/spark/spark_worker.py b/sentry_sdk/integrations/spark/spark_worker.py index cd4eb0f28b..632e870973 100644 --- a/sentry_sdk/integrations/spark/spark_worker.py +++ b/sentry_sdk/integrations/spark/spark_worker.py @@ -58,7 +58,7 @@ def _capture_exception(exc_info, hub): if rv: rv.reverse() hint = event_hint_with_exc_info(exc_info) - event = {"level": "error", "exception": {"values": rv}} + event = {"level": "error", "exception": {"values": rv}} # type: Event _tag_task_context() diff --git a/sentry_sdk/session.py b/sentry_sdk/session.py index 45e2236ec9..65c965b638 100644 --- a/sentry_sdk/session.py +++ b/sentry_sdk/session.py @@ -11,7 +11,7 @@ from typing import Any from typing import Dict - from sentry_sdk._types import SessionStatus + from sentry_sdk._types import Event, SessionStatus def _minute_trunc(ts): @@ -155,7 +155,7 @@ def get_json_attrs( return attrs def to_json(self): - # type: (...) -> Any + # type: (...) -> Event rv = { "sid": str(self.sid), "init": True, diff --git a/sentry_sdk/sessions.py b/sentry_sdk/sessions.py index 520fbbc059..e42af73d82 100644 --- a/sentry_sdk/sessions.py +++ b/sentry_sdk/sessions.py @@ -18,6 +18,8 @@ from typing import Optional from typing import Union + from sentry_sdk._types import Event + def is_auto_session_tracking_enabled(hub=None): # type: (Optional[sentry_sdk.Hub]) -> Union[Any, bool, None] @@ -55,7 +57,7 @@ def auto_session_tracking(hub=None, session_mode="application"): def make_aggregate_envelope(aggregate_states, attrs): - # type: (Any, Any) -> Any + # type: (Any, Any) -> Event return {"attrs": dict(attrs), "aggregates": list(aggregate_states.values())} diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index c811d2d2fe..4ed0e60425 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -68,7 +68,12 @@ Union, ) - from sentry_sdk._types import EndpointType, ExcInfo + from sentry_sdk._types import ( + EndpointType, + Event, + ExcInfo, + Hint, + ) epoch = datetime(1970, 1, 1) @@ -1053,7 +1058,7 @@ def event_from_exception( client_options=None, # type: Optional[Dict[str, Any]] mechanism=None, # type: Optional[Dict[str, Any]] ): - # type: (...) -> Tuple[Dict[str, Any], Dict[str, Any]] + # type: (...) -> Tuple[Event, Hint] exc_info = exc_info_from_error(exc_info) hint = event_hint_with_exc_info(exc_info) return (