From d726d86731998a7d08d5d0b05bf3df22976fd353 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 2 Apr 2024 12:15:59 +0200 Subject: [PATCH 1/2] fix(types): Fixed `Event | None` runtime `TypeError` --- sentry_sdk/types.py | 6 ++++-- tests/test_types.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/test_types.py diff --git a/sentry_sdk/types.py b/sentry_sdk/types.py index 9a96ed489f..16c57ceea4 100644 --- a/sentry_sdk/types.py +++ b/sentry_sdk/types.py @@ -13,9 +13,11 @@ if TYPE_CHECKING: from sentry_sdk._types import Event, Hint 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 = None - Hint = None + Event = Any + Hint = Any __all__ = ("Event", "Hint") diff --git a/tests/test_types.py b/tests/test_types.py new file mode 100644 index 0000000000..d06542c5e0 --- /dev/null +++ b/tests/test_types.py @@ -0,0 +1,17 @@ +from sentry_sdk.types import Event, Hint + + +def test_event_or_none_runtime(): + """ + Ensures that the `Event` type's runtime value supports the `|` operation with `None`. + This test is needed to ensure that using an `Event | None` type hint (e.g. for + `before_send`'s return value) does not raise a TypeError at runtime. + """ + Event | None + + +def test_hint_or_none_runtime(): + """ + Analogue to `test_event_or_none_runtime`, but for the `Hint` type. + """ + Hint | None From 725a80012682106b0d297680851d2de0717cbaa0 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 2 Apr 2024 12:41:01 +0200 Subject: [PATCH 2/2] Skip tests below 3.10 --- tests/test_types.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_types.py b/tests/test_types.py index d06542c5e0..bef6aaa59e 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1,6 +1,13 @@ +import sys + +import pytest from sentry_sdk.types import Event, Hint +@pytest.mark.skipif( + sys.version_info < (3, 10), + reason="Type hinting with `|` is available in Python 3.10+", +) def test_event_or_none_runtime(): """ Ensures that the `Event` type's runtime value supports the `|` operation with `None`. @@ -10,6 +17,10 @@ def test_event_or_none_runtime(): Event | None +@pytest.mark.skipif( + sys.version_info < (3, 10), + reason="Type hinting with `|` is available in Python 3.10+", +) def test_hint_or_none_runtime(): """ Analogue to `test_event_or_none_runtime`, but for the `Hint` type.