Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Encode `decimal_string` values in fixed-point notation
semver_level: patch
---

- `Decimal` and `float` values for `decimal_string` fields are now sent in fixed-point notation. Previously, small and large values used exponent notation, which V2 endpoints reject: `0.00001` was sent as `"1e-05"`, `1e16` as `"1e+16"`, and `Decimal("0.0000001")` as `"1E-7"`. Now they are sent as `"0.00001"`, `"10000000000000000"`, and `"0.0000001"`.
- `Decimal` values in `StripeObject` JSON output use fixed-point notation as well.
8 changes: 8 additions & 0 deletions stripe/_encode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import calendar
import datetime
import math
import time
from collections import OrderedDict
from decimal import Decimal
Expand All @@ -16,6 +17,13 @@ def _encode_datetime(dttime: datetime.datetime):


def _encode_decimal(dec) -> str:
# str() uses exponent notation for small and large values (str(0.00001) is
# "1e-05" and str(Decimal("0.0000001")) is "1E-7"), which the API rejects.
if isinstance(dec, float) and math.isfinite(dec):
# repr() keeps the float's shortest round-trip digits, so 0.1 stays "0.1"
dec = Decimal(repr(dec))
if isinstance(dec, Decimal) and dec.is_finite():
return format(dec, "f")
return str(dec)


Expand Down
60 changes: 59 additions & 1 deletion tests/test_decimal_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
- Response hydration: JSON string → Decimal
"""

import json
from decimal import Decimal

from stripe._encode import _coerce_decimal_string, _coerce_v2_params
from stripe._encode import (
_coerce_decimal_string,
_coerce_v2_params,
_make_suitable_for_json,
)
from stripe._stripe_object import StripeObject


Expand Down Expand Up @@ -88,6 +93,59 @@ def test_negative(self):
assert _coerce_decimal_string("-1.5", encode=False) == Decimal("-1.5")


class TestDecimalStringFixedPoint:
"""The API rejects exponent notation, so values are encoded in fixed-point."""

def test_small_decimal(self):
assert (
_coerce_decimal_string(Decimal("0.0000001"), encode=True)
== "0.0000001"
)
assert (
_coerce_decimal_string(Decimal("1E-7"), encode=True) == "0.0000001"
)
assert (
_coerce_decimal_string(Decimal("-2.5E-7"), encode=True)
== "-0.00000025"
)

def test_decimal_with_positive_exponent(self):
assert _coerce_decimal_string(Decimal("1E+3"), encode=True) == "1000"

def test_small_float(self):
assert _coerce_decimal_string(0.00001, encode=True) == "0.00001"
assert _coerce_decimal_string(-2.5e-7, encode=True) == "-0.00000025"

def test_large_float(self):
assert _coerce_decimal_string(1e16, encode=True) == "10000000000000000"
assert (
_coerce_decimal_string(1.5e19, encode=True)
== "15000000000000000000"
)

def test_float_keeps_shortest_digits(self):
assert _coerce_decimal_string(0.1, encode=True) == "0.1"
assert (
_coerce_decimal_string(1e15, encode=True) == "1000000000000000.0"
)

def test_non_finite_unchanged(self):
assert _coerce_decimal_string(float("nan"), encode=True) == "nan"
assert _coerce_decimal_string(float("inf"), encode=True) == "inf"
assert _coerce_decimal_string(Decimal("NaN"), encode=True) == "NaN"

def test_list(self):
assert _coerce_decimal_string(
[Decimal("1E-7"), 1e16], encode=True
) == ["0.0000001", "10000000000000000"]

def test_json_request_body(self):
body = json.dumps(
{"price": Decimal("1E-7")}, default=_make_suitable_for_json
)
assert body == '{"price": "0.0000001"}'


class TestCoerceV2ParamsDecimalString:
"""Tests for outbound request coercion (Decimal → str)."""

Expand Down