-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
There doesn't seem to be a way of robustly relying on library's types for API responses #1344
Comments
Thanks for reporting @matt-dalton We are aware of this issue and it is in our backlog to address it. |
what I've done locally for this is: # this _technically_ exists, but is inaccessible because it collides with dict.items(), so you have to
# use the __getitem__ method to access it
# items: list[StripeSubscriptionItem] # the items included in this subscription (prices live here)
def __getitem__(self, item: typing.Literal["items"]) -> StripeList[SubscriptionItem]:
pass (This comes up for In our codebase we want to use dot notation for all cases unless we are forced to use @overload
def __getitem__(self, item: Literal["items"] -> List[SubscriptionItem]:
...
@overload
def __getitem__(self, item: Literal["customer"] -> str | Customer:
... and so on for each field; maybe this is feasible if these types are all auto generated rather than hand maintained good luck! |
Same issue, we are converting over from JSON dictionary access to use the library fields and this was confusing but now I'm gunshy about trusting the library. |
We're soliciting feedback around some potential solutions to the |
Describe the bug
As described in this issue we can use either python dot or dict notation for accessing API response values.
For the types included in the library, dot notation successfully returns the correct types:
![Screenshot 2024-06-10 at 16 30 43](https://private-user-images.githubusercontent.com/22496337/338249952-f6a8980b-4337-4570-9ce9-a77869bbe766.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2MTI5MjcsIm5iZiI6MTczOTYxMjYyNywicGF0aCI6Ii8yMjQ5NjMzNy8zMzgyNDk5NTItZjZhODk4MGItNDMzNy00NTcwLTljZTktYTc3ODY5YmJlNzY2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDA5NDM0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWVlYTA4ZDUxYjIxNzEzMWFjMDRiOGZlN2MwZmZlNzhkMjRmZmM0YTgzNTY2MjQ2ZmE0OTBiYjY0ODM4ODA2OTUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.rDw1M7OxAtNS_9nN8RcSOQxyqQfHNmm6VlTQl26CqHM)
whereas using dict notation results in everything you access being set to
![Screenshot 2024-06-10 at 16 31 36](https://private-user-images.githubusercontent.com/22496337/338250223-793cdf35-e2f7-4e7f-a0d4-92822cc06413.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2MTI5MjcsIm5iZiI6MTczOTYxMjYyNywicGF0aCI6Ii8yMjQ5NjMzNy8zMzgyNTAyMjMtNzkzY2RmMzUtZTJmNy00ZTdmLWEwZDQtOTI4MjJjYzA2NDEzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDA5NDM0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY5ZTJjMTc4ZDRkZTQyMTI2ZTM4MWMwZWQ1MWRkZTg2ZTE0M2IwZTNhZGU0YmJmMzIxZGRkMjdkMTQwYzdhYmImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.sGjYxJfle_u29ZAY-Yb_Fm7w1RU5B4E-kz6FBIkQRHo)
Any
This is fine, however there's an issue with using dot notation (also mentioned in the other issue) where calling
subscription.items
results in a crash because it tries to access dict'sitems()
function instead.This means the types can't be relied upon. It also means there's no way of using the types without people knowing they need to access
items
in a different way.Is there a workaround for this? Or a fix?
To Reproduce
plan = subscription.items.data[0].plan
. It will crash, but the plan type will be set correctlyplan = subscription['items']['data'][0]['plan']
. It will work, but the types will be wrong.Expected behavior
At least one of the following should be happen (ideally both):
Code snippets
No response
OS
macOS
Language version
python 3.8.10
Library version
stripe-python 7.14.0
API version
2023-10-16
Additional context
No response
The text was updated successfully, but these errors were encountered: