Skip to content

Commit b92b2ae

Browse files
committed
ref: Clean up get_active_propagation_context
No longer returns `None`
1 parent 1ac88a0 commit b92b2ae

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

sentry_sdk/scope.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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.get_traceparent()
552-
553-
# TODO-neel will never happen
554-
return None
549+
return self.get_active_propagation_context().get_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,8 @@ 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+
# isolation scope always has a propagation_context so this cast is safe
655+
return typing.cast(PropagationContext, isolation_scope._propagation_context)
672656

673657
def clear(self):
674658
# type: () -> None
@@ -1057,10 +1041,11 @@ def start_transaction(
10571041
# update the sample rate in the dsc
10581042
if transaction.sample_rate is not None:
10591043
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)
1044+
baggage = propagation_context.baggage
1045+
1046+
if baggage is not None:
1047+
baggage.sentry_items["sample_rate"] = str(transaction.sample_rate)
1048+
10641049
if transaction._baggage:
10651050
transaction._baggage.sentry_items["sample_rate"] = str(
10661051
transaction.sample_rate
@@ -1134,8 +1119,7 @@ def start_span(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
11341119
# New spans get the `trace_id` from the scope
11351120
if "trace_id" not in kwargs:
11361121
propagation_context = self.get_active_propagation_context()
1137-
if propagation_context is not None:
1138-
kwargs["trace_id"] = propagation_context.trace_id
1122+
kwargs["trace_id"] = propagation_context.trace_id
11391123

11401124
span = Span(**kwargs)
11411125
else:

0 commit comments

Comments
 (0)