Skip to content

Fix DeprecationWarning when encoding StripeObject metadata (fixes #1651)#1687

Merged
ramya-stripe merged 8 commits intostripe:masterfrom
praniketkw:fix-metadata-deprecation-warning
Dec 23, 2025
Merged

Fix DeprecationWarning when encoding StripeObject metadata (fixes #1651)#1687
ramya-stripe merged 8 commits intostripe:masterfrom
praniketkw:fix-metadata-deprecation-warning

Conversation

@praniketkw
Copy link
Contributor

@praniketkw praniketkw commented Nov 25, 2025

Fixes #1651

Why?

When updating metadata on resources like invoices, users encountered a DeprecationWarning even though they were using the public API correctly. The warning was triggered internally when the encoder checked for the deprecated stripe_id property on StripeObject instances. While the deprecation warning serves an important purpose for direct user access to stripe_id, it should not be shown for internal library usage.

What?

  • Modified the _api_encode function in stripe/_encode.py to suppress DeprecationWarning when internally accessing the stripe_id property.
  • Added warnings.catch_warnings() context managers around both the hasattr check and the property access to prevent warnings from bubbling up to users during internal encoding operations.
  • Preserved the deprecation warning for direct user access to stripe_id while silencing it only for internal library usage.
  • Updated tests to verify that no deprecation warnings are raised during encoding operations while maintaining all existing encoding behavior.
  • The approach maintains the intended deprecation strategy while fixing the user experience issue where legitimate API usage triggered internal warnings.

@praniketkw praniketkw requested a review from a team as a code owner November 25, 2025 18:42
@praniketkw praniketkw requested review from xavdid-stripe and removed request for a team November 25, 2025 18:42
@ramya-stripe ramya-stripe requested review from prathmesh-stripe and removed request for xavdid-stripe December 2, 2025 03:02
@ramya-stripe ramya-stripe requested review from ramya-stripe and removed request for prathmesh-stripe December 9, 2025 18:21
@ramya-stripe
Copy link
Contributor

@praniketkw Can you also update the PR description based on the new approach?

@praniketkw
Copy link
Contributor Author

@ramya-stripe Done!

@ramya-stripe ramya-stripe merged commit 96545ff into stripe:master Dec 23, 2025
17 checks passed
@ramya-stripe
Copy link
Contributor

@praniketkw Thanks for your work on this. Can you update the PR description to reflect the final approach we have taken here?

@boris-savic
Copy link

Hi. This merge broke certain projects, because you have changed the behavior. Before it has checked the objects stripe_id property and now ti checks id - which can and indeed broke things.

image

@ramya-stripe
Copy link
Contributor

@boris-savic Internally, the stripe_id property reads from the id property. See https://github.com/praniketkw/stripe-python/blob/master/stripe/_stripe_object.py#L555

So both these properties should have the same value.
Can you expand as to how this is breaking for you?

@boris-savic
Copy link

In 14.1.0, if an object was passed in PaymentIntent.create for the customer=some_object the stripe library read the stripe_id property stored in some_object.stripe_id. After 14.2.0 this is no longer true as it reads the some_object.id and obviously breaks.

@prathmesh-stripe
Copy link
Contributor

@boris-savic I was looking at the PaymentIntent.create API and the customer key requires a string and not an object.
Our types also ask the customer to be a string and not an object.
Can you please provide an example with code sample of what you are trying to do just so that we can understand what breaks.

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 this pull request may close these issues.

Direct update metadata causes DeprecationWarning

4 participants