From 161bde42042ecf9e1434de71e0bd492b78f21f8e Mon Sep 17 00:00:00 2001 From: Safal Gupta <148842438+alphasafal@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:36:13 +0000 Subject: [PATCH 1/2] fix(encode): encode bool and datetime list elements like scalars _api_encode converts top-level and nested-dict bool and datetime values (bool -> "true"/"false", datetime -> unix timestamp) but yielded them raw when they appeared as direct elements of a list. A list param therefore serialized booleans as "True"/"False" and datetimes as their str() repr instead of the forms the API expects. Handle bool and datetime in the list branch the same way as at the top level. Adds regression tests. --- stripe/_encode.py | 4 ++++ tests/test_encode.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/stripe/_encode.py b/stripe/_encode.py index 7c402cf52..3230063cf 100644 --- a/stripe/_encode.py +++ b/stripe/_encode.py @@ -171,6 +171,10 @@ def _api_encode( ) for k, v in _api_encode(subdict): yield (k, v) + elif isinstance(sv, datetime.datetime): + yield (encoded_key, _encode_datetime(sv)) + elif isinstance(sv, bool): + yield (encoded_key, str(sv).lower()) else: yield (encoded_key, sv) elif isinstance(value, dict) or hasattr(value, "_data"): diff --git a/tests/test_encode.py b/tests/test_encode.py index f8f036c8f..5e39339e3 100644 --- a/tests/test_encode.py +++ b/tests/test_encode.py @@ -1,3 +1,4 @@ +import datetime import warnings from stripe._stripe_object import StripeObject @@ -86,6 +87,19 @@ def test_encode_boolean_value(self): result = list(_api_encode({"active": True, "deleted": False})) assert result == [("active", "true"), ("deleted", "false")] + def test_encode_list_with_boolean_values(self): + """Booleans inside a list are encoded as lowercase strings, like top-level booleans.""" + result = list(_api_encode({"flags": [True, False]})) + assert result == [("flags[0]", "true"), ("flags[1]", "false")] + + def test_encode_list_with_datetime_values(self): + """Datetimes inside a list are encoded as unix timestamps, like top-level datetimes.""" + dt = datetime.datetime( + 2024, 1, 2, 3, 4, 5, tzinfo=datetime.timezone.utc + ) + result = list(_api_encode({"created": [dt]})) + assert result == [("created[0]", 1704164645)] + def test_encode_list_value(self): """Test that list values are encoded with indexed keys.""" result = list(_api_encode({"items": ["item1", "item2", "item3"]})) From 68e3d5954024bde21efb9dbb2916fd044f3e0598 Mon Sep 17 00:00:00 2001 From: Safal Gupta <148842438+alphasafal@users.noreply.github.com> Date: Thu, 12 Nov 2026 10:30:09 +0530 Subject: [PATCH 2/2] Add changefile for list bool/datetime encoding fix --- .../2026-11-12_alphasafal_encode-list-bool-datetime.change.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .hark/changes/2026-11-12_alphasafal_encode-list-bool-datetime.change.md diff --git a/.hark/changes/2026-11-12_alphasafal_encode-list-bool-datetime.change.md b/.hark/changes/2026-11-12_alphasafal_encode-list-bool-datetime.change.md new file mode 100644 index 000000000..e029c90de --- /dev/null +++ b/.hark/changes/2026-11-12_alphasafal_encode-list-bool-datetime.change.md @@ -0,0 +1,4 @@ +--- +title: "Encode `bool` and `datetime` elements of list parameters the same way as top-level values (`true`/`false` and unix timestamps) instead of sending Python's `str()` form" +semver_level: patch +---