From 142f79c673cf73f3bca666c42c5c4ddabcda532e Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 27 Aug 2024 17:03:53 +0200 Subject: [PATCH 1/3] fix: Fix mypy "indented block" error Fix this [mypy error](https://github.com/getsentry/sentry-python/actions/runs/10390581522/job/28771379618): ``` sentry_sdk/integrations/opentelemetry/potel_span_processor.py:149: error: expected an indented block after function definition on line 31 [syntax] ``` Honestly not sure why this change fixes the problem, maybe there is some bug in `mypy`. --- sentry_sdk/integrations/opentelemetry/potel_span_processor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/opentelemetry/potel_span_processor.py b/sentry_sdk/integrations/opentelemetry/potel_span_processor.py index 8b2a2f4c36..8d6ec77eef 100644 --- a/sentry_sdk/integrations/opentelemetry/potel_span_processor.py +++ b/sentry_sdk/integrations/opentelemetry/potel_span_processor.py @@ -145,7 +145,7 @@ def _root_span_to_transaction_event(self, span): "transaction_info": {"source": "custom"}, "contexts": contexts, } - ) # type: Event + ) return event From efee140907d58921e51d1f79991b3877544d5470 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 27 Aug 2024 17:09:16 +0200 Subject: [PATCH 2/3] fix: Fix other mypy syntax failures After fixing the previous mypy failure, mypy discovered more syntax problems, which this commit fixes. --- sentry_sdk/integrations/opentelemetry/scope.py | 2 +- sentry_sdk/scope.py | 2 +- sentry_sdk/tracing.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/scope.py b/sentry_sdk/integrations/opentelemetry/scope.py index c1eacc3852..50a7e45b01 100644 --- a/sentry_sdk/integrations/opentelemetry/scope.py +++ b/sentry_sdk/integrations/opentelemetry/scope.py @@ -43,10 +43,10 @@ def _get_current_scope(cls): @classmethod def get_isolation_scope(cls): + # type: () -> Scope """ Returns the isolation scope. """ - # type: () -> Scope return cls._get_isolation_scope() or _INITIAL_ISOLATION_SCOPE @classmethod diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index acf7d2b83e..2154904d90 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -769,8 +769,8 @@ def span(self): @span.setter def span(self, span): - """Set current tracing span.""" # type: (Optional[Span]) -> None + """Set current tracing span.""" self._span = span # XXX: this differs from the implementation in JS, there Scope.setSpan # does not set Scope._transactionName. diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index c04b51a344..00b29554b1 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -1363,7 +1363,7 @@ def sampled(self): @sampled.setter def sampled(self, value): - # type: () -> Optional[bool] + # type: (Optional[bool]) -> None pass @property From fe542db3d81da52fb83919e0ea979efa6c64a124 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 27 Aug 2024 17:17:40 +0200 Subject: [PATCH 3/3] fix: Correct typing in `potel_span_processor` Fixing the original mypy error broke the typing; this change fixes the typing. --- .../integrations/opentelemetry/potel_span_processor.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/potel_span_processor.py b/sentry_sdk/integrations/opentelemetry/potel_span_processor.py index 8d6ec77eef..db4c1f58d6 100644 --- a/sentry_sdk/integrations/opentelemetry/potel_span_processor.py +++ b/sentry_sdk/integrations/opentelemetry/potel_span_processor.py @@ -1,4 +1,5 @@ from collections import deque, defaultdict +from typing import cast from opentelemetry.trace import format_trace_id, format_span_id from opentelemetry.context import Context @@ -154,7 +155,10 @@ def _span_to_json(self, span): if not span.context: return None - span_json = self._common_span_transaction_attributes_as_json(span) + # This is a safe cast because dict[str, Any] is a superset of Event + span_json = cast( + "dict[str, Any]", self._common_span_transaction_attributes_as_json(span) + ) if span_json is None: return None @@ -184,14 +188,14 @@ def _span_to_json(self, span): return span_json def _common_span_transaction_attributes_as_json(self, span): - # type: (ReadableSpan) -> Optional[dict[str, Any]] + # type: (ReadableSpan) -> Optional[Event] if not span.start_time or not span.end_time: return None common_json = { "start_timestamp": convert_from_otel_timestamp(span.start_time), "timestamp": convert_from_otel_timestamp(span.end_time), - } # type: dict[str, Any] + } # type: Event measurements = extract_span_attributes(span, SentrySpanAttribute.MEASUREMENT) if measurements: