⚡ Solution Summary
- The typechecker is correct; it sees the type as Dict[str, Any].
- Accessing properties like event.data.object.id works at runtime.
- To avoid type errors, consider using type stubs or suppressing specific warnings.
### Describe the bug
I see several examples you have of doing basically my snippet below, such https://docs.stripe.com/webhooks/process-undelivered-events).
But for me, Pyright is failing on basic attribute access. I have double checked all my software versions.
In the snipped, VSCode hints me that event is an Event, data is a Data, and object is a dict[str, Any] consistent with my results. When I option-click to drill in I indeed get:
class Data(StripeObject):
object: Dict[str, Any]
I am not sure if I am somehow doing something basically wrong here. If so I'd love to know what it is, even if it is just missing where the static type check failure is documented.
If not, is it _possible that the code actually runs per your example but doesn't support type checking? Maybe you have some __getattr__ interception for properties that Pyright cannot pick up?
If not, is your suggestion to use ClassName.contruct_event(event_obj) or something else?
### To Reproduce
```py
try:
# same result when using stripe.Webhook.construct_event
event = stripe.StripeClient(api_key=api_key).construct_event(
payload=payload, sig_header=signature_header, secret=webhook_secret
)
except ValueError as e:
raise BadRequestError(f"Invalid payload from Stripe {e}")
except stripe.SignatureVerificationError:
raise BadRequestError("Invalid signature on Stripe request")
base_obj = event.data.object
# Pyland in VSCode and Pyright from shell report "cannot access attribute id for dict[str, Any]"
# Same occurs if it is in a conditional (if event.type == "....)
fail = base_obj.id
```
### Expected behavior
accessing "id" passes static type analysis
### Code snippets
```Python
```
### OS
15.3.2
### Language version
Python 3.12
### Library version
stripe-python-12.1
### API version
Whatever SDK 12 sets by default
### Additional context
Pyright 1.1.400
relevant pyproject.toml:
```toml
[tool.pyright]
include = ["src/**/*.py"]
exclude = ["tests/**/*.py"]
pythonVersion = "3.12"
reportMissingTypeStubs = true
reportGeneralTypeIssues = "error"
```
Discussion & Fixes