|
1 | 1 | from typing import TYPE_CHECKING, TypeVar, Union |
2 | 2 |
|
3 | | - |
4 | 3 | # Re-exported for compat, since code out there in the wild might use this variable. |
5 | 4 | MYPY = TYPE_CHECKING |
6 | 5 |
|
@@ -93,20 +92,14 @@ def substituted_because_contains_sensitive_data(cls) -> "AnnotatedValue": |
93 | 92 |
|
94 | 93 | if TYPE_CHECKING: |
95 | 94 | from collections.abc import Container, MutableMapping, Sequence |
96 | | - |
97 | 95 | from datetime import datetime |
98 | | - |
99 | 96 | from types import TracebackType |
100 | | - from typing import Any |
101 | | - from typing import Callable |
102 | | - from typing import Dict |
103 | | - from typing import Mapping |
104 | | - from typing import NotRequired |
105 | | - from typing import Optional |
106 | | - from typing import Tuple |
107 | | - from typing import Type |
| 97 | + from typing import Any, Callable, Dict, Mapping, NotRequired, Optional, Type |
| 98 | + |
108 | 99 | from typing_extensions import Literal, TypedDict |
109 | 100 |
|
| 101 | + from sentry_sdk.tracing import TransactionSource |
| 102 | + |
110 | 103 | class SDKInfo(TypedDict): |
111 | 104 | name: str |
112 | 105 | version: str |
@@ -285,8 +278,64 @@ class SDKInfo(TypedDict): |
285 | 278 | # TODO: Make a proper type definition for this (PRs welcome!) |
286 | 279 | BreadcrumbHint = Dict[str, Any] |
287 | 280 |
|
288 | | - # TODO: Make a proper type definition for this (PRs welcome!) |
289 | | - SamplingContext = Dict[str, Any] |
| 281 | + _ASGIInfo = TypedDict("_ASGIInfo", {"version": str, "spec_version": str}) |
| 282 | + _ASGIScope = TypedDict( |
| 283 | + "_ASGIScope", |
| 284 | + { |
| 285 | + "type": str, |
| 286 | + "asgi": _ASGIInfo, |
| 287 | + "http_version": str, |
| 288 | + "server": tuple[str, int], |
| 289 | + "client": tuple[str, int], |
| 290 | + "scheme": str, |
| 291 | + "method": str, |
| 292 | + "root_path": str, |
| 293 | + "path": str, |
| 294 | + "raw_path": bytes, |
| 295 | + "query_string": bytes, |
| 296 | + "headers": list[tuple[bytes, bytes]], |
| 297 | + "state": dict[str, Any], |
| 298 | + }, |
| 299 | + ) |
| 300 | + _TransactionContext = TypedDict( |
| 301 | + "_TransactionContext", |
| 302 | + { |
| 303 | + "trace_id": str, |
| 304 | + "span_id": str, |
| 305 | + "parent_span_id": Optional[str], |
| 306 | + "same_process_as_parent": bool, |
| 307 | + "op": Optional[str], |
| 308 | + "description": Optional[str], |
| 309 | + "start_timestamp": datetime | int, |
| 310 | + "timestamp": Optional[datetime], |
| 311 | + "origin": str, |
| 312 | + "tags": NotRequired[dict[str, str]], |
| 313 | + "data": dict[str, Any], |
| 314 | + "name": str, |
| 315 | + "source": TransactionSource, |
| 316 | + "sampled": Optional[bool], |
| 317 | + }, |
| 318 | + ) |
| 319 | + _SamplingContextTyped = TypedDict( |
| 320 | + "_SamplingContextTyped", |
| 321 | + { |
| 322 | + "transaction_context": _TransactionContext, |
| 323 | + "parent_sampled": Optional[bool], |
| 324 | + "asgi_scope": NotRequired[_ASGIScope], |
| 325 | + # `wsgi_environ` is only present for WSGI server (like Django), and it contains mainly env vars, and some wsgi specifics. |
| 326 | + "wsgi_environ": NotRequired[dict[str, Any]], |
| 327 | + # `aiohttp_request` is only present for AIOHTTP server, and contains <class 'aiohttp.web_request.Request'> |
| 328 | + "aiohttp_request": NotRequired[Any], |
| 329 | + # NOT tested these below, but documented for completeness sake to pass mypy. |
| 330 | + "tornado_request": NotRequired[Any], |
| 331 | + "celery_job": NotRequired[dict[str, Any]], |
| 332 | + "rq_job": NotRequired[Any], |
| 333 | + "aws_event": NotRequired[Any], |
| 334 | + "aws_context": NotRequired[Any], |
| 335 | + "gcp_env": NotRequired[dict[str, Any]], |
| 336 | + }, |
| 337 | + ) |
| 338 | + SamplingContext = Union[_SamplingContextTyped, dict[str, Any]] |
290 | 339 |
|
291 | 340 | EventProcessor = Callable[[Event, Hint], Optional[Event]] |
292 | 341 | ErrorProcessor = Callable[[Event, ExcInfo], Optional[Event]] |
|
0 commit comments