Skip to content

Commit 336f7d5

Browse files
fix(types): Fixed Event | None runtime TypeError (#2928)
Change Event's runtime value to typing.Any, since the previous value of None caused the expression Event | None to result in a TypeError at runtime, even when the Event | None expression was used as a type hint. Also, add a test to make sure we don't reintroduce this bug. Fixes GH-2926
1 parent fd8a9b2 commit 336f7d5

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

sentry_sdk/types.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
if TYPE_CHECKING:
1414
from sentry_sdk._types import Event, Hint
1515
else:
16+
from typing import Any
17+
1618
# The lines below allow the types to be imported from outside `if TYPE_CHECKING`
1719
# guards. The types in this module are only intended to be used for type hints.
18-
Event = None
19-
Hint = None
20+
Event = Any
21+
Hint = Any
2022

2123
__all__ = ("Event", "Hint")

tests/test_types.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
3+
import pytest
4+
from sentry_sdk.types import Event, Hint
5+
6+
7+
@pytest.mark.skipif(
8+
sys.version_info < (3, 10),
9+
reason="Type hinting with `|` is available in Python 3.10+",
10+
)
11+
def test_event_or_none_runtime():
12+
"""
13+
Ensures that the `Event` type's runtime value supports the `|` operation with `None`.
14+
This test is needed to ensure that using an `Event | None` type hint (e.g. for
15+
`before_send`'s return value) does not raise a TypeError at runtime.
16+
"""
17+
Event | None
18+
19+
20+
@pytest.mark.skipif(
21+
sys.version_info < (3, 10),
22+
reason="Type hinting with `|` is available in Python 3.10+",
23+
)
24+
def test_hint_or_none_runtime():
25+
"""
26+
Analogue to `test_event_or_none_runtime`, but for the `Hint` type.
27+
"""
28+
Hint | None

0 commit comments

Comments
 (0)