Skip to content

Commit

Permalink
Fix del statement to not raise KeyError (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe authored Aug 6, 2019
1 parent 76c35ae commit 2a20652
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stripe/stripe_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def __delitem__(self, k):
super(StripeObject, self).__delitem__(k)

# Allows for unpickling in Python 3.x
if hasattr(self, "_unsaved_values"):
if hasattr(self, "_unsaved_values") and k in self._unsaved_values:
self._unsaved_values.remove(k)

# Custom unpickling method that uses `update` to update the dictionary
Expand Down
11 changes: 11 additions & 0 deletions tests/test_stripe_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ def test_deletion(self):
obj.refresh_from({"coupon": "foo"}, api_key="bar", partial=True)
assert obj.coupon == "foo"

def test_deletion_metadata(self):
obj = stripe.stripe_object.StripeObject.construct_from(
{"metadata": {"key": "value"}}, "mykey"
)

assert obj.metadata["key"] == "value"

del obj.metadata["key"]
with pytest.raises(KeyError):
obj.metadata["key"]

def test_copy(self):
nested = stripe.stripe_object.StripeObject.construct_from(
{"value": "bar"}, "mykey"
Expand Down

0 comments on commit 2a20652

Please sign in to comment.