From 6d441a11f61517f28e55ee1283f091bcdf6441e5 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Thu, 10 Apr 2025 13:31:04 +0200 Subject: [PATCH 1/2] ref(scope): Properly type `Scope.root_span` Currently, this property has type `Any`, but it can now be changed to `Optional[Span]` Depends on: - #4263 --- sentry_sdk/integrations/huey.py | 16 ++++++++++------ sentry_sdk/scope.py | 3 +-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/sentry_sdk/integrations/huey.py b/sentry_sdk/integrations/huey.py index fcdbd4f9f9..322f1b28a9 100644 --- a/sentry_sdk/integrations/huey.py +++ b/sentry_sdk/integrations/huey.py @@ -110,11 +110,13 @@ def _capture_exception(exc_info): # type: (ExcInfo) -> None scope = sentry_sdk.get_current_scope() - if exc_info[0] in HUEY_CONTROL_FLOW_EXCEPTIONS: - scope.root_span.set_status(SPANSTATUS.ABORTED) - return + if scope.root_span is not None: + if exc_info[0] in HUEY_CONTROL_FLOW_EXCEPTIONS: + scope.root_span.set_status(SPANSTATUS.ABORTED) + return + + scope.root_span.set_status(SPANSTATUS.INTERNAL_ERROR) - scope.root_span.set_status(SPANSTATUS.INTERNAL_ERROR) event, hint = event_from_exception( exc_info, client_options=sentry_sdk.get_client().options, @@ -135,8 +137,10 @@ def _sentry_execute(*args, **kwargs): exc_info = sys.exc_info() _capture_exception(exc_info) reraise(*exc_info) - else: - sentry_sdk.get_current_scope().root_span.set_status(SPANSTATUS.OK) + + root_span = sentry_sdk.get_current_scope().root_span + if root_span is not None: + root_span.set_status(SPANSTATUS.OK) return result diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 487b45b583..be2933ef33 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -689,8 +689,7 @@ def fingerprint(self, value): @property def root_span(self): - # type: () -> Any - # would be type: () -> Optional[Span], see https://github.com/python/mypy/issues/3004 + # type: () -> Optional[Span] """Return the root span in the scope, if any.""" # there is no span/transaction on the scope From b26e0095ef8b6c5647a73710e388e16488436891 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Wed, 9 Apr 2025 17:11:50 +0200 Subject: [PATCH 2/2] ref: Simplify `Scope.root_span` logic --- sentry_sdk/scope.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index be2933ef33..1e6757a9de 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -691,16 +691,9 @@ def fingerprint(self, value): def root_span(self): # type: () -> Optional[Span] """Return the root span in the scope, if any.""" - - # there is no span/transaction on the scope if self._span is None: return None - # there is an orphan span on the scope - if self._span.root_span is None: - return None - # there is either a root span (which is its own root - # span) or a non-orphan span on the scope return self._span.root_span def set_transaction_name(self, name, source=None):