Skip to content

Commit 2bcd641

Browse files
committed
First step in cleaning up types
1 parent 1e69dab commit 2bcd641

File tree

8 files changed

+156
-161
lines changed

8 files changed

+156
-161
lines changed

scripts/populate_tox/config.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@
159159
},
160160
"statsig": {
161161
"package": "statsig",
162-
"deps": {
163-
"*": ["typing_extensions"],
164-
},
165162
},
166163
"strawberry": {
167164
"package": "strawberry-graphql[fastapi,flask]",

sentry_sdk/_types.py

Lines changed: 130 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
from typing import TYPE_CHECKING, TypeVar, Union
1+
from datetime import datetime
2+
from collections.abc import Container, MutableMapping, Sequence
3+
4+
from types import TracebackType
5+
from typing import (
6+
TYPE_CHECKING,
7+
TypeVar,
8+
Union,
9+
Any,
10+
Callable,
11+
Dict,
12+
Literal,
13+
Mapping,
14+
NotRequired,
15+
Optional,
16+
Type,
17+
TypedDict,
18+
)
219

320

421
# Re-exported for compat, since code out there in the wild might use this variable.
@@ -96,146 +113,131 @@ def substituted_because_contains_sensitive_data(cls):
96113
T = TypeVar("T")
97114
Annotated = Union[AnnotatedValue, T]
98115

116+
Hint = Dict[str, Any]
117+
Log = TypedDict(
118+
"Log",
119+
{
120+
"severity_text": str,
121+
"severity_number": int,
122+
"body": str,
123+
"attributes": dict[str, str | bool | float | int],
124+
"time_unix_nano": int,
125+
"trace_id": Optional[str],
126+
},
127+
)
128+
129+
# "critical" is an alias of "fatal" recognized by Relay
130+
LogLevelStr = Literal["fatal", "critical", "error", "warning", "info", "debug"]
131+
132+
DurationUnit = Literal[
133+
"nanosecond",
134+
"microsecond",
135+
"millisecond",
136+
"second",
137+
"minute",
138+
"hour",
139+
"day",
140+
"week",
141+
]
142+
143+
InformationUnit = Literal[
144+
"bit",
145+
"byte",
146+
"kilobyte",
147+
"kibibyte",
148+
"megabyte",
149+
"mebibyte",
150+
"gigabyte",
151+
"gibibyte",
152+
"terabyte",
153+
"tebibyte",
154+
"petabyte",
155+
"pebibyte",
156+
"exabyte",
157+
"exbibyte",
158+
]
159+
160+
FractionUnit = Literal["ratio", "percent"]
161+
MeasurementUnit = Union[DurationUnit, InformationUnit, FractionUnit, str]
162+
163+
MeasurementValue = TypedDict(
164+
"MeasurementValue",
165+
{
166+
"value": float,
167+
"unit": NotRequired[Optional[MeasurementUnit]],
168+
},
169+
)
170+
171+
Breadcrumb = Dict[str, Any]
172+
BreadcrumbHint = Dict[str, Any]
173+
174+
SamplingContext = Dict[str, Any]
175+
176+
Event = TypedDict(
177+
"Event",
178+
{
179+
"breadcrumbs": Annotated[
180+
dict[Literal["values"], list[dict[str, Any]]]
181+
], # TODO: We can expand on this type
182+
"check_in_id": str,
183+
"contexts": dict[str, dict[str, object]],
184+
"dist": str,
185+
"duration": Optional[float],
186+
"environment": str,
187+
"errors": list[dict[str, Any]], # TODO: We can expand on this type
188+
"event_id": str,
189+
"exception": dict[
190+
Literal["values"], list[dict[str, Any]]
191+
], # TODO: We can expand on this type
192+
"extra": MutableMapping[str, object],
193+
"fingerprint": list[str],
194+
"level": LogLevelStr,
195+
"logentry": Mapping[str, object],
196+
"logger": str,
197+
"measurements": dict[str, MeasurementValue],
198+
"message": str,
199+
"modules": dict[str, str],
200+
"monitor_config": Mapping[str, object],
201+
"monitor_slug": Optional[str],
202+
"platform": Literal["python"],
203+
"profile": object, # Should be sentry_sdk.profiler.Profile, but we can't import that here due to circular imports
204+
"release": str,
205+
"request": dict[str, object],
206+
"sdk": Mapping[str, object],
207+
"server_name": str,
208+
"spans": Annotated[list[dict[str, object]]],
209+
"stacktrace": dict[
210+
str, object
211+
], # We access this key in the code, but I am unsure whether we ever set it
212+
"start_timestamp": datetime,
213+
"status": Optional[str],
214+
"tags": MutableMapping[str, str], # Tags must be less than 200 characters each
215+
"threads": dict[
216+
Literal["values"], list[dict[str, Any]]
217+
], # TODO: We can expand on this type
218+
"timestamp": Optional[datetime], # Must be set before sending the event
219+
"transaction": str,
220+
"transaction_info": Mapping[str, Any], # TODO: We can expand on this type
221+
"type": Literal["check_in", "transaction"],
222+
"user": dict[str, object],
223+
"_dropped_spans": int,
224+
},
225+
total=False,
226+
)
99227

100-
if TYPE_CHECKING:
101-
from collections.abc import Container, MutableMapping, Sequence
102-
103-
from datetime import datetime
104228

105-
from types import TracebackType
106-
from typing import Any
107-
from typing import Callable
108-
from typing import Dict
109-
from typing import Mapping
110-
from typing import NotRequired
111-
from typing import Optional
112-
from typing import Type
113-
from typing_extensions import Literal, TypedDict
229+
if TYPE_CHECKING:
114230

115231
class SDKInfo(TypedDict):
116232
name: str
117233
version: str
118234
packages: Sequence[Mapping[str, str]]
119235

120-
# "critical" is an alias of "fatal" recognized by Relay
121-
LogLevelStr = Literal["fatal", "critical", "error", "warning", "info", "debug"]
122-
123-
DurationUnit = Literal[
124-
"nanosecond",
125-
"microsecond",
126-
"millisecond",
127-
"second",
128-
"minute",
129-
"hour",
130-
"day",
131-
"week",
132-
]
133-
134-
InformationUnit = Literal[
135-
"bit",
136-
"byte",
137-
"kilobyte",
138-
"kibibyte",
139-
"megabyte",
140-
"mebibyte",
141-
"gigabyte",
142-
"gibibyte",
143-
"terabyte",
144-
"tebibyte",
145-
"petabyte",
146-
"pebibyte",
147-
"exabyte",
148-
"exbibyte",
149-
]
150-
151-
FractionUnit = Literal["ratio", "percent"]
152-
MeasurementUnit = Union[DurationUnit, InformationUnit, FractionUnit, str]
153-
154-
MeasurementValue = TypedDict(
155-
"MeasurementValue",
156-
{
157-
"value": float,
158-
"unit": NotRequired[Optional[MeasurementUnit]],
159-
},
160-
)
161-
162-
Event = TypedDict(
163-
"Event",
164-
{
165-
"breadcrumbs": Annotated[
166-
dict[Literal["values"], list[dict[str, Any]]]
167-
], # TODO: We can expand on this type
168-
"check_in_id": str,
169-
"contexts": dict[str, dict[str, object]],
170-
"dist": str,
171-
"duration": Optional[float],
172-
"environment": str,
173-
"errors": list[dict[str, Any]], # TODO: We can expand on this type
174-
"event_id": str,
175-
"exception": dict[
176-
Literal["values"], list[dict[str, Any]]
177-
], # TODO: We can expand on this type
178-
"extra": MutableMapping[str, object],
179-
"fingerprint": list[str],
180-
"level": LogLevelStr,
181-
"logentry": Mapping[str, object],
182-
"logger": str,
183-
"measurements": dict[str, MeasurementValue],
184-
"message": str,
185-
"modules": dict[str, str],
186-
"monitor_config": Mapping[str, object],
187-
"monitor_slug": Optional[str],
188-
"platform": Literal["python"],
189-
"profile": object, # Should be sentry_sdk.profiler.Profile, but we can't import that here due to circular imports
190-
"release": str,
191-
"request": dict[str, object],
192-
"sdk": Mapping[str, object],
193-
"server_name": str,
194-
"spans": Annotated[list[dict[str, object]]],
195-
"stacktrace": dict[
196-
str, object
197-
], # We access this key in the code, but I am unsure whether we ever set it
198-
"start_timestamp": datetime,
199-
"status": Optional[str],
200-
"tags": MutableMapping[
201-
str, str
202-
], # Tags must be less than 200 characters each
203-
"threads": dict[
204-
Literal["values"], list[dict[str, Any]]
205-
], # TODO: We can expand on this type
206-
"timestamp": Optional[datetime], # Must be set before sending the event
207-
"transaction": str,
208-
"transaction_info": Mapping[str, Any], # TODO: We can expand on this type
209-
"type": Literal["check_in", "transaction"],
210-
"user": dict[str, object],
211-
"_dropped_spans": int,
212-
},
213-
total=False,
214-
)
215-
216236
ExcInfo = Union[
217237
tuple[Type[BaseException], BaseException, Optional[TracebackType]],
218238
tuple[None, None, None],
219239
]
220240

221-
Hint = Dict[str, Any]
222-
Log = TypedDict(
223-
"Log",
224-
{
225-
"severity_text": str,
226-
"severity_number": int,
227-
"body": str,
228-
"attributes": dict[str, str | bool | float | int],
229-
"time_unix_nano": int,
230-
"trace_id": Optional[str],
231-
},
232-
)
233-
234-
Breadcrumb = Dict[str, Any]
235-
BreadcrumbHint = Dict[str, Any]
236-
237-
SamplingContext = Dict[str, Any]
238-
239241
EventProcessor = Callable[[Event, Hint], Optional[Event]]
240242
ErrorProcessor = Callable[[Event, ExcInfo], Optional[Event]]
241243
BreadcrumbProcessor = Callable[[Breadcrumb, BreadcrumbHint], Optional[Breadcrumb]]

sentry_sdk/consts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ class CompressionAlgo(Enum):
3232
from typing import Optional
3333
from typing import Callable
3434
from typing import Union
35+
from typing import Literal
3536
from typing import List
3637
from typing import Type
3738
from typing import Dict
3839
from typing import Any
3940
from typing import Sequence
4041
from typing import Tuple
41-
from typing_extensions import Literal
42-
from typing_extensions import TypedDict
42+
from typing import TypedDict
4343

4444
from sentry_sdk._types import (
4545
BreadcrumbProcessor,

sentry_sdk/integrations/_asgi_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
if TYPE_CHECKING:
99
from typing import Any
1010
from typing import Dict
11+
from typing import Literal
1112
from typing import Optional
1213
from typing import Union
13-
from typing_extensions import Literal
1414

1515
from sentry_sdk.utils import AnnotatedValue
1616

sentry_sdk/profiler/continuous_profiler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
from typing import Optional
3535
from typing import Set
3636
from typing import Type
37+
from typing import TypedDict
3738
from typing import Union
38-
from typing_extensions import TypedDict
39+
3940
from sentry_sdk._types import ContinuousProfilerMode, SDKInfo
4041
from sentry_sdk.profiler.utils import (
4142
ExtractedSample,

sentry_sdk/profiler/transaction_profiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
from typing import Optional
6363
from typing import Set
6464
from typing import Type
65-
from typing_extensions import TypedDict
65+
from typing import TypedDict
6666

6767
from sentry_sdk.profiler.utils import (
6868
ProcessedStack,

sentry_sdk/profiler/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from typing import Optional
1515
from typing import Sequence
1616
from typing import Tuple
17-
from typing_extensions import TypedDict
17+
from typing import TypedDict
1818

1919
ThreadId = str
2020

sentry_sdk/types.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
"""
2-
This module contains type definitions for the Sentry SDK's public API.
3-
The types are re-exported from the internal module `sentry_sdk._types`.
1+
# Re-exported from sentry_sdk._types to make those types public
2+
from sentry_sdk._types import (
3+
Event,
4+
EventDataCategory,
5+
Hint,
6+
Log,
7+
Breadcrumb,
8+
BreadcrumbHint,
9+
SamplingContext,
10+
)
411

5-
Disclaimer: Since types are a form of documentation, type definitions
6-
may change in minor releases. Removing a type would be considered a
7-
breaking change, and so we will only remove type definitions in major
8-
releases.
9-
"""
10-
11-
from typing import TYPE_CHECKING
12-
13-
if TYPE_CHECKING:
14-
from sentry_sdk._types import Event, EventDataCategory, Hint, Log
15-
else:
16-
from typing import Any
17-
18-
# The lines below allow the types to be imported from outside `if TYPE_CHECKING`
19-
# guards. The types in this module are only intended to be used for type hints.
20-
Event = Any
21-
EventDataCategory = Any
22-
Hint = Any
23-
Log = Any
24-
25-
__all__ = ("Event", "EventDataCategory", "Hint", "Log")
12+
__all__ = (
13+
"Event",
14+
"EventDataCategory",
15+
"Hint",
16+
"Log",
17+
"Breadcrumb",
18+
"BreadcrumbHint",
19+
"SamplingContext",
20+
)

0 commit comments

Comments
 (0)