getsentry / getsentry/sentry-python

Don't export `Any` instead of instantiable types

Open
#4,656 5 comments 0 reactions 0 assignees View on GitHub
Component: Typing Errors Improvement Python
Dominant language
Python
Stars
2.2k
Forks
669
Avg merge
1d 40m
Merged PRs (30d)
212

Description

`sentry_sdk/types.py` exports types for hinting, but some of the types it exports in type-checking mode can be instantiated. This creates a dangerous mismatch between the type-checking and runtime API where i.e, `Event()` will appear valid due to being a `TypedDict`, but will raise an `Any type cannot be instantiated` error at runtime.

I don't think there's an easy solution for hinting the type checker about hint-only types, but also I'm not sure why the `_types.py` code is behind `if TYPE_CHECKING:`. My recommendation would either be:
* Just remove the `if TYPE_CHECKING:` or,
* Export instantiable substitutes versions of the instantiable types (maybe just TypedDict types but not sure)

https://github.com/getsentry/sentry-python/blob/84adbb74e27b7716cbaaddbb299a44fe3fbcd6a6/sentry_sdk/types.py#L28-L37

Contributor guide

Open the contributing guide

Research direction

Start with the TYPE_CHECKING block in sentry_sdk/types.py and the referenced _types.py code. Reproduce the mismatch described for Event(), then compare the type-checking exports with the runtime API. Done means the exported typing behavior no longer suggests that hint-only types can be instantiated at runtime.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.