-
Notifications
You must be signed in to change notification settings - Fork 475
Fix DeprecationWarning when encoding StripeObject metadata (fixes #1651) #1687
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
Merged
ramya-stripe
merged 8 commits into
stripe:master
from
praniketkw:fix-metadata-deprecation-warning
Dec 23, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f72f1e5
Fix DeprecationWarning when encoding StripeObject metadata (fixes #1651)
praniketkw 6e0cc8e
Merge branch 'master' into fix-metadata-deprecation-warning
praniketkw 767807a
Suppress deprecation warnings during internal encoding to fix metadat…
praniketkw c838192
Merge branch 'master' into fix-metadata-deprecation-warning
praniketkw 84823f3
Use getattr for id access to avoid stripe_id deprecation warning
praniketkw 9feb0b3
Handle None id values correctly in encoding
praniketkw 85e98f4
Simplify to check hasattr(value, id) directly
praniketkw ff6c538
Merge branch 'master' into fix-metadata-deprecation-warning
ramya-stripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import warnings | ||
|
|
||
| from stripe._stripe_object import StripeObject | ||
| from stripe._encode import _api_encode | ||
|
|
||
|
|
||
| class TestApiEncode: | ||
| def test_encode_stripe_object_without_id_no_deprecation_warning(self): | ||
| """ | ||
| Test that encoding a StripeObject without an id (like metadata) | ||
| does not trigger a deprecation warning. | ||
| Regression test for issue #1651. | ||
| """ | ||
| metadata = StripeObject() | ||
| metadata["key1"] = "value1" | ||
| metadata["key2"] = "value2" | ||
|
|
||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| result = list(_api_encode({"metadata": metadata})) | ||
|
|
||
| # Check no deprecation warnings were raised | ||
| deprecation_warnings = [ | ||
| warning | ||
| for warning in w | ||
| if issubclass(warning.category, DeprecationWarning) | ||
| ] | ||
| assert len(deprecation_warnings) == 0, ( | ||
| f"Expected no deprecation warnings, but got {len(deprecation_warnings)}" | ||
| ) | ||
|
|
||
| # Verify the metadata was encoded correctly as nested dict | ||
| assert result == [ | ||
| ("metadata[key1]", "value1"), | ||
| ("metadata[key2]", "value2"), | ||
| ] | ||
|
|
||
| def test_encode_stripe_object_with_id_extracts_id(self): | ||
| """ | ||
| Test that encoding a StripeObject with an id (like a customer reference) | ||
| correctly extracts just the id value. | ||
| """ | ||
| customer = StripeObject() | ||
| customer["id"] = "cus_123" | ||
| customer["name"] = "Test Customer" | ||
|
|
||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| result = list(_api_encode({"customer": customer})) | ||
|
|
||
| # Check no deprecation warnings were raised | ||
| deprecation_warnings = [ | ||
| warning | ||
| for warning in w | ||
| if issubclass(warning.category, DeprecationWarning) | ||
| ] | ||
| assert len(deprecation_warnings) == 0, ( | ||
| f"Expected no deprecation warnings, but got {len(deprecation_warnings)}" | ||
| ) | ||
|
|
||
| # Should encode to just the ID | ||
| assert result == [("customer", "cus_123")] | ||
|
|
||
| def test_encode_regular_dict(self): | ||
| """Test that regular dicts are encoded as nested dicts.""" | ||
| regular_dict = {"key1": "value1", "key2": "value2"} | ||
| result = list(_api_encode({"data": regular_dict})) | ||
|
|
||
| assert result == [ | ||
| ("data[key1]", "value1"), | ||
| ("data[key2]", "value2"), | ||
| ] | ||
|
|
||
| def test_encode_none_value_skipped(self): | ||
| """Test that None values are skipped during encoding.""" | ||
| result = list(_api_encode({"field": None})) | ||
| assert result == [] | ||
|
|
||
| def test_encode_string_value(self): | ||
| """Test that string values are encoded directly.""" | ||
| result = list(_api_encode({"name": "John Doe"})) | ||
| assert result == [("name", "John Doe")] | ||
|
|
||
| def test_encode_boolean_value(self): | ||
| """Test that boolean values are encoded as lowercase strings.""" | ||
| result = list(_api_encode({"active": True, "deleted": False})) | ||
| assert result == [("active", "true"), ("deleted", "false")] | ||
|
|
||
| def test_encode_list_value(self): | ||
| """Test that list values are encoded with indexed keys.""" | ||
| result = list(_api_encode({"items": ["item1", "item2", "item3"]})) | ||
| assert result == [ | ||
| ("items[0]", "item1"), | ||
| ("items[1]", "item2"), | ||
| ("items[2]", "item3"), | ||
| ] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.