Skip to content

Commit 203c816

Browse files
committed
use type method instead of isinstance
1 parent 10e96aa commit 203c816

4 files changed

Lines changed: 50 additions & 51 deletions

File tree

sentry_sdk/integrations/fastapi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import sys
22
from copy import deepcopy
33
from functools import wraps
4+
from typing import TYPE_CHECKING
45

56
import sentry_sdk
67
from sentry_sdk.integrations import DidNotEnable
78
from sentry_sdk.scope import should_send_default_pii
8-
from sentry_sdk.traces import _is_sampled_streamed_span
9+
from sentry_sdk.traces import _is_streamed_span
910
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource
1011
from sentry_sdk.tracing_utils import has_span_streaming_enabled
1112
from sentry_sdk.utils import transaction_from_function
1213

13-
from typing import TYPE_CHECKING
14-
1514
if TYPE_CHECKING:
1615
from typing import Any, Callable, Dict
16+
1717
from sentry_sdk._types import Event
1818

1919
try:
@@ -95,7 +95,7 @@ def _sentry_call(*args: "Any", **kwargs: "Any") -> "Any":
9595
if has_span_streaming_enabled(client.options):
9696
current_span = current_scope.streamed_span
9797

98-
if _is_sampled_streamed_span(current_span):
98+
if _is_streamed_span(current_span):
9999
segment = current_span._segment
100100
segment._update_active_thread()
101101

sentry_sdk/integrations/starlette.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import functools
22
import json
3-
import warnings
43
import sys
4+
import warnings
55
from collections.abc import Set
66
from copy import deepcopy
77
from json import JSONDecodeError
8+
from typing import TYPE_CHECKING
89

910
import sentry_sdk
1011
from sentry_sdk.consts import OP
1112
from sentry_sdk.integrations import (
13+
_DEFAULT_FAILED_REQUEST_STATUS_CODES,
1214
DidNotEnable,
1315
Integration,
14-
_DEFAULT_FAILED_REQUEST_STATUS_CODES,
1516
)
1617
from sentry_sdk.integrations._wsgi_common import (
1718
DEFAULT_HTTP_METHODS_TO_CAPTURE,
@@ -21,7 +22,7 @@
2122
)
2223
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
2324
from sentry_sdk.scope import should_send_default_pii
24-
from sentry_sdk.traces import _get_current_streamed_span, _is_sampled_streamed_span
25+
from sentry_sdk.traces import _get_current_streamed_span, _is_streamed_span
2526
from sentry_sdk.tracing import (
2627
SOURCE_FOR_STYLE,
2728
TransactionSource,
@@ -36,8 +37,6 @@
3637
transaction_from_function,
3738
)
3839

39-
from typing import TYPE_CHECKING
40-
4140
if TYPE_CHECKING:
4241
from typing import Any, Awaitable, Callable, Container, Dict, Optional, Tuple, Union
4342

@@ -54,7 +53,8 @@
5453
)
5554
from starlette.requests import Request # type: ignore
5655
from starlette.routing import Match # type: ignore
57-
from starlette.types import ASGIApp, Receive, Scope as StarletteScope, Send # type: ignore
56+
from starlette.types import ASGIApp, Receive, Send # type: ignore
57+
from starlette.types import Scope as StarletteScope
5858
except ImportError:
5959
raise DidNotEnable("Starlette is not installed")
6060

@@ -255,7 +255,7 @@ def _set_request_body_data_on_streaming_segment(
255255
info: "Optional[Dict[str, Any]]",
256256
) -> None:
257257
current_span = _get_current_streamed_span()
258-
if info and "data" in info and _is_sampled_streamed_span(current_span):
258+
if info and "data" in info and _is_streamed_span(current_span):
259259
with capture_internal_exceptions():
260260
current_span._segment.set_attribute(
261261
"http.request.body.data",
@@ -552,7 +552,7 @@ def _sentry_sync_func(*args: "Any", **kwargs: "Any") -> "Any":
552552
if span_streaming:
553553
current_span = current_scope.streamed_span
554554

555-
if _is_sampled_streamed_span(current_span):
555+
if _is_streamed_span(current_span):
556556
current_span._segment._update_active_thread()
557557
elif current_scope.transaction is not None:
558558
current_scope.transaction.update_active_thread()

sentry_sdk/scope.py

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import os
22
import sys
33
import warnings
4-
from copy import copy, deepcopy
54
from collections import deque
65
from contextlib import contextmanager
7-
from enum import Enum
6+
from copy import copy, deepcopy
87
from datetime import datetime, timezone
8+
from enum import Enum
99
from functools import wraps
1010
from itertools import chain
11+
from typing import TYPE_CHECKING, cast
1112

1213
import sentry_sdk
1314
from sentry_sdk._types import AnnotatedValue
@@ -18,27 +19,19 @@
1819
INSTRUMENTER,
1920
SPANDATA,
2021
)
21-
from sentry_sdk.feature_flags import FlagBuffer, DEFAULT_FLAG_CAPACITY
22+
from sentry_sdk.feature_flags import DEFAULT_FLAG_CAPACITY, FlagBuffer
2223
from sentry_sdk.profiler.continuous_profiler import (
2324
get_profiler_id,
2425
try_autostart_continuous_profiler,
2526
try_profile_lifecycle_trace_start,
2627
)
2728
from sentry_sdk.profiler.transaction_profiler import Profile
2829
from sentry_sdk.session import Session
29-
from sentry_sdk.tracing_utils import (
30-
Baggage,
31-
has_tracing_enabled,
32-
has_span_streaming_enabled,
33-
is_ignored_span,
34-
_make_sampling_decision,
35-
PropagationContext,
36-
)
3730
from sentry_sdk.traces import (
3831
_DEFAULT_PARENT_SPAN,
39-
StreamedSpan,
4032
NoOpStreamedSpan,
41-
_is_sampled_streamed_span,
33+
StreamedSpan,
34+
_is_streamed_span,
4235
)
4336
from sentry_sdk.tracing import (
4437
BAGGAGE_HEADER_NAME,
@@ -47,40 +40,48 @@
4740
Span,
4841
Transaction,
4942
)
43+
from sentry_sdk.tracing_utils import (
44+
Baggage,
45+
PropagationContext,
46+
_make_sampling_decision,
47+
has_span_streaming_enabled,
48+
has_tracing_enabled,
49+
is_ignored_span,
50+
)
5051
from sentry_sdk.utils import (
52+
ContextVar,
5153
capture_internal_exception,
5254
capture_internal_exceptions,
53-
ContextVar,
5455
datetime_from_isoformat,
5556
disable_capture_event,
5657
event_from_exception,
5758
exc_info_from_error,
5859
format_attribute,
59-
logger,
6060
has_logs_enabled,
6161
has_metrics_enabled,
62+
logger,
6263
)
6364

64-
from typing import TYPE_CHECKING, cast
65-
6665
if TYPE_CHECKING:
6766
from collections.abc import Mapping
68-
69-
from typing import Any
70-
from typing import Callable
71-
from typing import Deque
72-
from typing import Dict
73-
from typing import Generator
74-
from typing import Iterator
75-
from typing import List
76-
from typing import Optional
77-
from typing import ParamSpec
78-
from typing import Tuple
79-
from typing import TypeVar
80-
from typing import Union
67+
from typing import (
68+
Any,
69+
Callable,
70+
Deque,
71+
Dict,
72+
Generator,
73+
Iterator,
74+
List,
75+
Optional,
76+
ParamSpec,
77+
Tuple,
78+
TypeVar,
79+
Union,
80+
)
8181

8282
from typing_extensions import Unpack
8383

84+
import sentry_sdk
8485
from sentry_sdk._types import (
8586
Attributes,
8687
AttributeValue,
@@ -97,11 +98,8 @@
9798
SamplingContext,
9899
Type,
99100
)
100-
101101
from sentry_sdk.tracing import TransactionKwargs
102102

103-
import sentry_sdk
104-
105103
P = ParamSpec("P")
106104
R = TypeVar("R")
107105

@@ -590,7 +588,7 @@ def get_traceparent(self, *args: "Any", **kwargs: "Any") -> "Optional[str]":
590588

591589
span_streaming = has_span_streaming_enabled(client.options)
592590
# If we have an active span, return traceparent from there
593-
if span_streaming and _is_sampled_streamed_span(self.streamed_span):
591+
if span_streaming and _is_streamed_span(self.streamed_span):
594592
return self.streamed_span._to_traceparent()
595593
elif not span_streaming and self.span is not None:
596594
return self.span._to_traceparent()
@@ -610,7 +608,7 @@ def get_baggage(self, *args: "Any", **kwargs: "Any") -> "Optional[Baggage]":
610608

611609
span_streaming = has_span_streaming_enabled(client.options)
612610
# If we have an active span, return baggage from there
613-
if span_streaming and _is_sampled_streamed_span(self.streamed_span):
611+
if span_streaming and _is_streamed_span(self.streamed_span):
614612
return self.streamed_span._to_baggage()
615613
elif not span_streaming and self.span is not None:
616614
return self.span._to_baggage()
@@ -915,7 +913,7 @@ def streamed_span(self, span: "Optional[StreamedSpan]") -> None:
915913

916914
# Also set _transaction and _transaction_info in streaming mode as this
917915
# is used for populating events and linking them to segments
918-
if _is_sampled_streamed_span(span) and span._is_segment():
916+
if _is_streamed_span(span) and span._is_segment():
919917
self._transaction = span.name
920918
if span._attributes.get("sentry.span.source"):
921919
self._transaction_info["source"] = str(

sentry_sdk/traces.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
TypeVar,
4242
Union,
4343
)
44+
4445
from sentry_sdk._types import Attributes, AttributeValue
4546
from sentry_sdk.profiler.continuous_profiler import ContinuousProfile
4647

@@ -766,9 +767,9 @@ def make_db_query(sql):
766767
return decorator
767768

768769

769-
def _is_sampled_streamed_span(span: "Any") -> "TypeGuard[StreamedSpan]":
770-
"""Returns True if span is a StreamedSpan that was sampled (not a NoOpStreamedSpan)."""
771-
return isinstance(span, StreamedSpan) and not isinstance(span, NoOpStreamedSpan)
770+
def _is_streamed_span(span: "Any") -> "TypeGuard[StreamedSpan]":
771+
"""Returns True if span is a StreamedSpan (not a NoOpStreamedSpan)."""
772+
return type(span) is StreamedSpan
772773

773774

774775
def _get_current_streamed_span(

0 commit comments

Comments
 (0)