Skip to content
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

del StripeOject.metadata['some_key'] deletes key AND raises KeyError #596

Closed
apinsard opened this issue Aug 2, 2019 · 3 comments · Fixed by #599
Closed

del StripeOject.metadata['some_key'] deletes key AND raises KeyError #596

apinsard opened this issue Aug 2, 2019 · 3 comments · Fixed by #599

Comments

@apinsard
Copy link

apinsard commented Aug 2, 2019

python: 3.5.3
stripe-python: 2.29.4

I just experienced quite unexpected KeyError exception the following code:

# pi is a `stripe.PaymentIntent`
if 'damage_deposit_amount' in pi.metadata:
    del pi.metadata['damage_deposit_amount']  # <- raises `KeyError`

According to the information I have in my error report 'damage_deposit_amount' is indeed not in pi.metadata. So, I first thought 'key' in StripeObject always returns True.

However, after testing it in a python console I figured out the following:

In [1]: from utils.stripe import stripe  # (= import stripe + stripe.api_key = settings.STRIPE['SECRET_KEY'])

In [2]: pi = stripe.PaymentIntent.retrieve('pi_deadbeef')

In [3]: pi.metadata
Out[3]: 
<StripeObject at 0x7f92140c5098> JSON: {
  "booking_id": "13506",
  "damage_deposit_amount": "20000",
  "waiting_for_webhook": "True"
}

In [4]: 'foobar' in pi.metadata
Out[4]: False

In [5]: del pi.metadata['damage_deposit_amount']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-e399e0ff7d29> in <module>
----> 1 del pi.metadata['damage_deposit_amount']

/usr/share/virtualenvs/cocoonr/lib/python3.5/site-packages/stripe/stripe_object.py in __delitem__(self, k)
    136         # Allows for unpickling in Python 3.x
    137         if hasattr(self, "_unsaved_values"):
--> 138             self._unsaved_values.remove(k)
    139
    140     # Custom unpickling method that uses `update` to update the dictionary

KeyError: 'damage_deposit_amount'

In [6]: pi.metadata
Out[6]: 
<StripeObject at 0x7f92140c5098> JSON: {
  "booking_id": "13506",
  "waiting_for_webhook": "True"
}

In [7]: pi.save()
Out[7]: 
<PaymentIntent payment_intent id=pi_deadbeef at 0x7f9214136598> JSON: {
  ...
  "metadata": {
    "booking_id": "13506",
    "waiting_for_webhook": "True"
  },
  ...
}

In [8]: pi = stripe.PaymentIntent.retrieve('pi_deadbeef')

In [9]: pi.metadata
Out[9]: 
<StripeObject at 0x7f92168b07c8> JSON: {
  "booking_id": "13506",
  "damage_deposit_amount": "20000",
  "waiting_for_webhook": "True"
}

In [10]:

To sum it up:

  1. del pi.metadata['damage_deposit_amount'] deleted the key, but raised a KeyError.
  2. Saving the object returned an object without the deleted key.
  3. Reloading the object showed that the key was actually not deleted.
@ob-stripe
Copy link
Contributor

Hi @apinsard, thanks for the report.

You can unset metadata keys like this:

stripe.PaymentIntent.modify('pi_deadbeef', metadata={'damage_deposit_amount': ''})

or like this:

pi = stripe.PaymentIntent.retrieve('pi_deadbeef')
pi.metadata['damage_deposit_amount'] = None
pi.save()

Nevertheless, the KeyError when using the del keyword is unexpected. We'll investigate and work on a fix (or at least a clearer error message).

@apinsard
Copy link
Author

apinsard commented Aug 2, 2019

Thank you for the quick answer and the provided workaround!

@ob-stripe
Copy link
Contributor

I just released version 2.33.1, using del should no longer throw a KeyError. Though note that to unset metadata keys you'd still need to use one of the two ways I showed in my previous message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants