diff --git a/scripts/populate_tox/config.py b/scripts/populate_tox/config.py index b1c61b6a14..c0323a82b5 100644 --- a/scripts/populate_tox/config.py +++ b/scripts/populate_tox/config.py @@ -159,9 +159,6 @@ }, "statsig": { "package": "statsig", - "deps": { - "*": ["typing_extensions"], - }, }, "strawberry": { "package": "strawberry-graphql[fastapi,flask]", diff --git a/sentry_sdk/_types.py b/sentry_sdk/_types.py index 6fb057d1a7..1cbff1eb20 100644 --- a/sentry_sdk/_types.py +++ b/sentry_sdk/_types.py @@ -1,4 +1,21 @@ -from typing import TYPE_CHECKING, TypeVar, Union +from datetime import datetime +from collections.abc import Container, MutableMapping, Sequence + +from types import TracebackType +from typing import ( + TYPE_CHECKING, + TypeVar, + Union, + Any, + Callable, + Dict, + Literal, + Mapping, + NotRequired, + Optional, + Type, + TypedDict, +) # Re-exported for compat, since code out there in the wild might use this variable. @@ -96,146 +113,147 @@ def substituted_because_contains_sensitive_data(cls): T = TypeVar("T") Annotated = Union[AnnotatedValue, T] +Hint = Dict[str, Any] +Log = TypedDict( + "Log", + { + "severity_text": str, + "severity_number": int, + "body": str, + "attributes": dict[str, str | bool | float | int], + "time_unix_nano": int, + "trace_id": Optional[str], + }, +) + +# "critical" is an alias of "fatal" recognized by Relay +LogLevelStr = Literal["fatal", "critical", "error", "warning", "info", "debug"] + +EventDataCategory = Literal[ + "default", + "error", + "crash", + "transaction", + "security", + "attachment", + "session", + "internal", + "profile", + "profile_chunk", + "monitor", + "span", + "log", +] + +DurationUnit = Literal[ + "nanosecond", + "microsecond", + "millisecond", + "second", + "minute", + "hour", + "day", + "week", +] + +InformationUnit = Literal[ + "bit", + "byte", + "kilobyte", + "kibibyte", + "megabyte", + "mebibyte", + "gigabyte", + "gibibyte", + "terabyte", + "tebibyte", + "petabyte", + "pebibyte", + "exabyte", + "exbibyte", +] + +FractionUnit = Literal["ratio", "percent"] +MeasurementUnit = Union[DurationUnit, InformationUnit, FractionUnit, str] + +MeasurementValue = TypedDict( + "MeasurementValue", + { + "value": float, + "unit": NotRequired[Optional[MeasurementUnit]], + }, +) + +Breadcrumb = Dict[str, Any] +BreadcrumbHint = Dict[str, Any] + +SamplingContext = Dict[str, Any] + +Event = TypedDict( + "Event", + { + "breadcrumbs": Annotated[ + dict[Literal["values"], list[dict[str, Any]]] + ], # TODO: We can expand on this type + "check_in_id": str, + "contexts": dict[str, dict[str, object]], + "dist": str, + "duration": Optional[float], + "environment": str, + "errors": list[dict[str, Any]], # TODO: We can expand on this type + "event_id": str, + "exception": dict[ + Literal["values"], list[dict[str, Any]] + ], # TODO: We can expand on this type + "extra": MutableMapping[str, object], + "fingerprint": list[str], + "level": LogLevelStr, + "logentry": Mapping[str, object], + "logger": str, + "measurements": dict[str, MeasurementValue], + "message": str, + "modules": dict[str, str], + "monitor_config": Mapping[str, object], + "monitor_slug": Optional[str], + "platform": Literal["python"], + "profile": object, # Should be sentry_sdk.profiler.Profile, but we can't import that here due to circular imports + "release": str, + "request": dict[str, object], + "sdk": Mapping[str, object], + "server_name": str, + "spans": Annotated[list[dict[str, object]]], + "stacktrace": dict[ + str, object + ], # We access this key in the code, but I am unsure whether we ever set it + "start_timestamp": datetime, + "status": Optional[str], + "tags": MutableMapping[str, str], # Tags must be less than 200 characters each + "threads": dict[ + Literal["values"], list[dict[str, Any]] + ], # TODO: We can expand on this type + "timestamp": Optional[datetime], # Must be set before sending the event + "transaction": str, + "transaction_info": Mapping[str, Any], # TODO: We can expand on this type + "type": Literal["check_in", "transaction"], + "user": dict[str, object], + "_dropped_spans": int, + }, + total=False, +) -if TYPE_CHECKING: - from collections.abc import Container, MutableMapping, Sequence - - from datetime import datetime - from types import TracebackType - from typing import Any - from typing import Callable - from typing import Dict - from typing import Mapping - from typing import NotRequired - from typing import Optional - from typing import Type - from typing_extensions import Literal, TypedDict +if TYPE_CHECKING: class SDKInfo(TypedDict): name: str version: str packages: Sequence[Mapping[str, str]] - # "critical" is an alias of "fatal" recognized by Relay - LogLevelStr = Literal["fatal", "critical", "error", "warning", "info", "debug"] - - DurationUnit = Literal[ - "nanosecond", - "microsecond", - "millisecond", - "second", - "minute", - "hour", - "day", - "week", - ] - - InformationUnit = Literal[ - "bit", - "byte", - "kilobyte", - "kibibyte", - "megabyte", - "mebibyte", - "gigabyte", - "gibibyte", - "terabyte", - "tebibyte", - "petabyte", - "pebibyte", - "exabyte", - "exbibyte", - ] - - FractionUnit = Literal["ratio", "percent"] - MeasurementUnit = Union[DurationUnit, InformationUnit, FractionUnit, str] - - MeasurementValue = TypedDict( - "MeasurementValue", - { - "value": float, - "unit": NotRequired[Optional[MeasurementUnit]], - }, - ) - - Event = TypedDict( - "Event", - { - "breadcrumbs": Annotated[ - dict[Literal["values"], list[dict[str, Any]]] - ], # TODO: We can expand on this type - "check_in_id": str, - "contexts": dict[str, dict[str, object]], - "dist": str, - "duration": Optional[float], - "environment": str, - "errors": list[dict[str, Any]], # TODO: We can expand on this type - "event_id": str, - "exception": dict[ - Literal["values"], list[dict[str, Any]] - ], # TODO: We can expand on this type - "extra": MutableMapping[str, object], - "fingerprint": list[str], - "level": LogLevelStr, - "logentry": Mapping[str, object], - "logger": str, - "measurements": dict[str, MeasurementValue], - "message": str, - "modules": dict[str, str], - "monitor_config": Mapping[str, object], - "monitor_slug": Optional[str], - "platform": Literal["python"], - "profile": object, # Should be sentry_sdk.profiler.Profile, but we can't import that here due to circular imports - "release": str, - "request": dict[str, object], - "sdk": Mapping[str, object], - "server_name": str, - "spans": Annotated[list[dict[str, object]]], - "stacktrace": dict[ - str, object - ], # We access this key in the code, but I am unsure whether we ever set it - "start_timestamp": datetime, - "status": Optional[str], - "tags": MutableMapping[ - str, str - ], # Tags must be less than 200 characters each - "threads": dict[ - Literal["values"], list[dict[str, Any]] - ], # TODO: We can expand on this type - "timestamp": Optional[datetime], # Must be set before sending the event - "transaction": str, - "transaction_info": Mapping[str, Any], # TODO: We can expand on this type - "type": Literal["check_in", "transaction"], - "user": dict[str, object], - "_dropped_spans": int, - }, - total=False, - ) - ExcInfo = Union[ tuple[Type[BaseException], BaseException, Optional[TracebackType]], tuple[None, None, None], ] - Hint = Dict[str, Any] - Log = TypedDict( - "Log", - { - "severity_text": str, - "severity_number": int, - "body": str, - "attributes": dict[str, str | bool | float | int], - "time_unix_nano": int, - "trace_id": Optional[str], - }, - ) - - Breadcrumb = Dict[str, Any] - BreadcrumbHint = Dict[str, Any] - - SamplingContext = Dict[str, Any] - EventProcessor = Callable[[Event, Hint], Optional[Event]] ErrorProcessor = Callable[[Event, ExcInfo], Optional[Event]] BreadcrumbProcessor = Callable[[Breadcrumb, BreadcrumbHint], Optional[Breadcrumb]] @@ -247,21 +265,6 @@ class SDKInfo(TypedDict): # https://github.com/python/mypy/issues/5710 NotImplementedType = Any - EventDataCategory = Literal[ - "default", - "error", - "crash", - "transaction", - "security", - "attachment", - "session", - "internal", - "profile", - "profile_chunk", - "monitor", - "span", - "log", - ] SessionStatus = Literal["ok", "exited", "crashed", "abnormal"] ContinuousProfilerMode = Literal["thread", "gevent", "unknown"] diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index 5fbd162299..bdeb666a32 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -32,14 +32,14 @@ class CompressionAlgo(Enum): from typing import Optional from typing import Callable from typing import Union + from typing import Literal from typing import List from typing import Type from typing import Dict from typing import Any from typing import Sequence from typing import Tuple - from typing_extensions import Literal - from typing_extensions import TypedDict + from typing import TypedDict from sentry_sdk._types import ( BreadcrumbProcessor, diff --git a/sentry_sdk/integrations/_asgi_common.py b/sentry_sdk/integrations/_asgi_common.py index 52ecdbfd58..5ad08880f1 100644 --- a/sentry_sdk/integrations/_asgi_common.py +++ b/sentry_sdk/integrations/_asgi_common.py @@ -8,9 +8,9 @@ if TYPE_CHECKING: from typing import Any from typing import Dict + from typing import Literal from typing import Optional from typing import Union - from typing_extensions import Literal from sentry_sdk.utils import AnnotatedValue diff --git a/sentry_sdk/profiler/continuous_profiler.py b/sentry_sdk/profiler/continuous_profiler.py index 371f61c632..16cbfccda3 100644 --- a/sentry_sdk/profiler/continuous_profiler.py +++ b/sentry_sdk/profiler/continuous_profiler.py @@ -34,8 +34,9 @@ from typing import Optional from typing import Set from typing import Type + from typing import TypedDict from typing import Union - from typing_extensions import TypedDict + from sentry_sdk._types import ContinuousProfilerMode, SDKInfo from sentry_sdk.profiler.utils import ( ExtractedSample, diff --git a/sentry_sdk/profiler/transaction_profiler.py b/sentry_sdk/profiler/transaction_profiler.py index 095ce2f2f9..de86674593 100644 --- a/sentry_sdk/profiler/transaction_profiler.py +++ b/sentry_sdk/profiler/transaction_profiler.py @@ -62,7 +62,7 @@ from typing import Optional from typing import Set from typing import Type - from typing_extensions import TypedDict + from typing import TypedDict from sentry_sdk.profiler.utils import ( ProcessedStack, diff --git a/sentry_sdk/profiler/utils.py b/sentry_sdk/profiler/utils.py index 3554cddb5d..59357844da 100644 --- a/sentry_sdk/profiler/utils.py +++ b/sentry_sdk/profiler/utils.py @@ -14,7 +14,7 @@ from typing import Optional from typing import Sequence from typing import Tuple - from typing_extensions import TypedDict + from typing import TypedDict ThreadId = str diff --git a/sentry_sdk/types.py b/sentry_sdk/types.py index 2b9f04c097..24d86b669e 100644 --- a/sentry_sdk/types.py +++ b/sentry_sdk/types.py @@ -1,25 +1,20 @@ -""" -This module contains type definitions for the Sentry SDK's public API. -The types are re-exported from the internal module `sentry_sdk._types`. +# Re-exported from sentry_sdk._types to make those types public +from sentry_sdk._types import ( + Event, + EventDataCategory, + Hint, + Log, + Breadcrumb, + BreadcrumbHint, + SamplingContext, +) -Disclaimer: Since types are a form of documentation, type definitions -may change in minor releases. Removing a type would be considered a -breaking change, and so we will only remove type definitions in major -releases. -""" - -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from sentry_sdk._types import Event, EventDataCategory, Hint, Log -else: - from typing import Any - - # The lines below allow the types to be imported from outside `if TYPE_CHECKING` - # guards. The types in this module are only intended to be used for type hints. - Event = Any - EventDataCategory = Any - Hint = Any - Log = Any - -__all__ = ("Event", "EventDataCategory", "Hint", "Log") +__all__ = ( + "Event", + "EventDataCategory", + "Hint", + "Log", + "Breadcrumb", + "BreadcrumbHint", + "SamplingContext", +)