Skip to content

Commit a1dab84

Browse files
authored
ref: Clean up get_active_propagation_context (#5217)
### Description No longer returns `None` since the last `isolation_scope` branch will always have one setup by the constructor. #### Issues * resolves: #5216 * resolves: PY-2011
1 parent d662111 commit a1dab84

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

sentry_sdk/scope.py

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
)
4646

4747
import typing
48-
from typing import TYPE_CHECKING
48+
from typing import TYPE_CHECKING, cast
4949

5050
if TYPE_CHECKING:
5151
from collections.abc import Mapping
@@ -546,12 +546,7 @@ def get_traceparent(self, *args, **kwargs):
546546
return self.span.to_traceparent()
547547

548548
# else return traceparent from the propagation context
549-
propagation_context = self.get_active_propagation_context()
550-
if propagation_context is not None:
551-
return propagation_context.to_traceparent()
552-
553-
# TODO-neel will never happen
554-
return None
549+
return self.get_active_propagation_context().to_traceparent()
555550

556551
def get_baggage(self, *args, **kwargs):
557552
# type: (Any, Any) -> Optional[Baggage]
@@ -566,12 +561,7 @@ def get_baggage(self, *args, **kwargs):
566561
return self.span.to_baggage()
567562

568563
# else return baggage from the propagation context
569-
propagation_context = self.get_active_propagation_context()
570-
if propagation_context is not None:
571-
return propagation_context.get_baggage()
572-
573-
# TODO-neel will never happen
574-
return None
564+
return self.get_active_propagation_context().get_baggage()
575565

576566
def get_trace_context(self):
577567
# type: () -> Dict[str, Any]
@@ -588,8 +578,6 @@ def get_trace_context(self):
588578
return {"trace_id": trace_id, "span_id": span_id}
589579

590580
propagation_context = self.get_active_propagation_context()
591-
if propagation_context is None:
592-
return {}
593581

594582
return {
595583
"trace_id": propagation_context.trace_id,
@@ -650,13 +638,11 @@ def iter_trace_propagation_headers(self, *args, **kwargs):
650638
for header in span.iter_headers():
651639
yield header
652640
else:
653-
propagation_context = self.get_active_propagation_context()
654-
if propagation_context is not None:
655-
for header in propagation_context.iter_headers():
656-
yield header
641+
for header in self.get_active_propagation_context().iter_headers():
642+
yield header
657643

658644
def get_active_propagation_context(self):
659-
# type: () -> Optional[PropagationContext]
645+
# type: () -> PropagationContext
660646
if self._propagation_context is not None:
661647
return self._propagation_context
662648

@@ -665,10 +651,10 @@ def get_active_propagation_context(self):
665651
return current_scope._propagation_context
666652

667653
isolation_scope = self.get_isolation_scope()
668-
if isolation_scope._propagation_context is not None:
669-
return isolation_scope._propagation_context
670-
671-
return None
654+
# should actually never happen, but just in case someone calls scope.clear
655+
if isolation_scope._propagation_context is None:
656+
isolation_scope._propagation_context = PropagationContext()
657+
return isolation_scope._propagation_context
672658

673659
def clear(self):
674660
# type: () -> None
@@ -1057,10 +1043,11 @@ def start_transaction(
10571043
# update the sample rate in the dsc
10581044
if transaction.sample_rate is not None:
10591045
propagation_context = self.get_active_propagation_context()
1060-
if propagation_context:
1061-
baggage = propagation_context.baggage
1062-
if baggage is not None:
1063-
baggage.sentry_items["sample_rate"] = str(transaction.sample_rate)
1046+
baggage = propagation_context.baggage
1047+
1048+
if baggage is not None:
1049+
baggage.sentry_items["sample_rate"] = str(transaction.sample_rate)
1050+
10641051
if transaction._baggage:
10651052
transaction._baggage.sentry_items["sample_rate"] = str(
10661053
transaction.sample_rate
@@ -1134,8 +1121,7 @@ def start_span(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
11341121
# New spans get the `trace_id` from the scope
11351122
if "trace_id" not in kwargs:
11361123
propagation_context = self.get_active_propagation_context()
1137-
if propagation_context is not None:
1138-
kwargs["trace_id"] = propagation_context.trace_id
1124+
kwargs["trace_id"] = propagation_context.trace_id
11391125

11401126
span = Span(**kwargs)
11411127
else:
@@ -1154,7 +1140,7 @@ def continue_trace(
11541140
self.generate_propagation_context(environ_or_headers)
11551141

11561142
# generate_propagation_context ensures that the propagation_context is not None.
1157-
propagation_context = typing.cast(PropagationContext, self._propagation_context)
1143+
propagation_context = cast(PropagationContext, self._propagation_context)
11581144

11591145
optional_kwargs = {}
11601146
if name:

0 commit comments

Comments
 (0)