Skip to content

Commit ba00e86

Browse files
committed
Fix some casts
1 parent ec1dc9b commit ba00e86

File tree

4 files changed

+17
-23
lines changed

4 files changed

+17
-23
lines changed

sentry_sdk/scrubber.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
iter_event_frames,
66
)
77

8-
from typing import TYPE_CHECKING, cast
8+
from typing import TYPE_CHECKING
99

1010
if TYPE_CHECKING:
11-
from typing import List, Dict, Optional
11+
from typing import List, Optional
1212
from sentry_sdk._types import Event
1313

1414

@@ -159,9 +159,10 @@ def scrub_frames(self, event: Event) -> None:
159159
def scrub_spans(self, event: Event) -> None:
160160
with capture_internal_exceptions():
161161
if "spans" in event:
162-
for span in cast("List[Dict[str, object]]", event["spans"]):
163-
if "data" in span:
164-
self.scrub_dict(span["data"])
162+
if not isinstance(event["spans"], AnnotatedValue):
163+
for span in event["spans"]:
164+
if "data" in span:
165+
self.scrub_dict(span["data"])
165166

166167
def scrub_event(self, event: Event) -> None:
167168
self.scrub_request(event)

sentry_sdk/tracing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
should_be_treated_as_error,
4747
)
4848

49-
from typing import TYPE_CHECKING, cast, overload
49+
from typing import TYPE_CHECKING, overload
5050

5151
if TYPE_CHECKING:
5252
from typing import (
@@ -290,8 +290,8 @@ def origin(self, value: Optional[str]) -> None:
290290

291291
@property
292292
def root_span(self) -> Optional[Span]:
293-
root_otel_span = cast(
294-
"Optional[OtelSpan]", get_sentry_meta(self._otel_span, "root_span")
293+
root_otel_span: Optional[OtelSpan] = get_sentry_meta(
294+
self._otel_span, "root_span"
295295
)
296296
return Span(otel_span=root_otel_span) if root_otel_span else None
297297

sentry_sdk/transport.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from sentry_sdk.worker import BackgroundWorker
3232
from sentry_sdk.envelope import Envelope, Item, PayloadRef
3333

34-
from typing import TYPE_CHECKING, cast
34+
from typing import TYPE_CHECKING
3535

3636
if TYPE_CHECKING:
3737
from typing import (
@@ -152,7 +152,7 @@ def __del__(self: Self) -> None:
152152

153153
def _parse_rate_limits(
154154
header: str, now: Optional[datetime] = None
155-
) -> Iterable[Tuple[Optional[EventDataCategory], datetime]]:
155+
) -> Iterable[Tuple[Optional[str], datetime]]:
156156
if now is None:
157157
now = datetime.now(timezone.utc)
158158

@@ -163,7 +163,6 @@ def _parse_rate_limits(
163163

164164
retry_after = now + timedelta(seconds=int(retry_after_val))
165165
for category in categories and categories.split(";") or (None,):
166-
category = cast("Optional[EventDataCategory]", category)
167166
yield category, retry_after
168167
except (LookupError, ValueError):
169168
continue
@@ -182,7 +181,7 @@ def __init__(self: Self, options: Dict[str, Any]) -> None:
182181
self.options: Dict[str, Any] = options
183182
self._worker = BackgroundWorker(queue_size=options["transport_queue_size"])
184183
self._auth = self.parsed_dsn.to_auth("sentry.python/%s" % VERSION)
185-
self._disabled_until: Dict[Optional[EventDataCategory], datetime] = {}
184+
self._disabled_until: Dict[Optional[str], datetime] = {}
186185
# We only use this Retry() class for the `get_retry_after` method it exposes
187186
self._retry = urllib3.util.Retry()
188187
self._discarded_events: DefaultDict[Tuple[EventDataCategory, str], int] = (
@@ -251,9 +250,7 @@ def record_lost_event(
251250
event = item.get_transaction_event() or {}
252251

253252
# +1 for the transaction itself
254-
span_count = (
255-
len(cast("List[Dict[str, object]]", event.get("spans") or [])) + 1
256-
)
253+
span_count = len(event.get("spans") or []) + 1
257254
self.record_lost_event(reason, "span", quantity=span_count)
258255

259256
elif data_category == "attachment":

sentry_sdk/utils.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,14 +1027,10 @@ def exc_info_from_error(error: Union[BaseException, ExcInfo]) -> ExcInfo:
10271027
else:
10281028
raise ValueError("Expected Exception object to report, got %s!" % type(error))
10291029

1030-
exc_info = (exc_type, exc_value, tb)
1031-
1032-
if TYPE_CHECKING:
1033-
# This cast is safe because exc_type and exc_value are either both
1034-
# None or both not None.
1035-
exc_info = cast("ExcInfo", exc_info)
1036-
1037-
return exc_info
1030+
if exc_type is not None and exc_value is not None:
1031+
return (exc_type, exc_value, tb)
1032+
else:
1033+
return (None, None, None)
10381034

10391035

10401036
def merge_stack_frames(

0 commit comments

Comments
 (0)