stripe / stripe-python

Python library for the Stripe API.
https://stripe.com
MIT License
1.64k stars 418 forks source link

There doesn't seem to be a way of robustly relying on library's types for API responses #1344

Open matt-dalton opened 2 months ago

matt-dalton commented 2 months ago

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

whereas using dict notation results in everything you access being set to Any Screenshot 2024-06-10 at 16 31 36

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's items() 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

  1. Use a python type checking system (we use pyright)
  2. Try and access the plan object from a subscription response using plan = subscription.items.data[0].plan. It will crash, but the plan type will be set correctly
  3. Instead, try accessing using plan = 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

ramya-stripe commented 2 months ago

Thanks for reporting @matt-dalton

We are aware of this issue and it is in our backlog to address it.

kurtbrose commented 1 month ago

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 SubscriptionSchedulePhase too, and possibly other places.)

In our codebase we want to use dot notation for all cases unless we are forced to use []; unfortunately for the more general case this would be much more of a PITA.

   @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!