Skip to content

Commit

Permalink
fix(types): Fixed Event | None runtime TypeError (#2928)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
szokeasaurusrex authored Apr 2, 2024
1 parent fd8a9b2 commit 336f7d5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions sentry_sdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
28 changes: 28 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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`.
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


@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.
"""
Hint | None

0 comments on commit 336f7d5

Please sign in to comment.