diff --git a/stripe/stripe_object.py b/stripe/stripe_object.py index 60d0fffb0..b97cb9832 100644 --- a/stripe/stripe_object.py +++ b/stripe/stripe_object.py @@ -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 diff --git a/tests/test_stripe_object.py b/tests/test_stripe_object.py index 8f1071168..be0bd39d7 100644 --- a/tests/test_stripe_object.py +++ b/tests/test_stripe_object.py @@ -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"