Skip to content

Fix _dump_float producing invalid TOML for Decimal special values#454

Open
bysiber wants to merge 1 commit intouiri:masterfrom
bysiber:fix/dump-float-decimal-special-values
Open

Fix _dump_float producing invalid TOML for Decimal special values#454
bysiber wants to merge 1 commit intouiri:masterfrom
bysiber:fix/dump-float-decimal-special-values

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

Summary

_dump_float produces invalid TOML when encoding Decimal special values (Infinity, -Infinity, NaN).

Problem

_dump_float uses "{}".format(v) to convert numeric values to strings. For float, Python produces lowercase inf/nan which matches TOML's expected format. But for Decimal, Python produces Infinity/NaN (capitalized), which is not valid TOML:

>>> import toml
>>> from decimal import Decimal
>>> toml.dumps({"val": Decimal("inf")})
'val = Infinity\n'
# Invalid TOML — should be 'val = inf\n'

>>> toml.loads('val = Infinity')
# TomlDecodeError: invalid literal for int() with base 0: 'Infinity'

Since _dump_float is registered in dump_funcs for both float and Decimal, it needs to handle both formats.

Fix

Normalize Infinityinf, -Infinity-inf, and NaNnan in _dump_float before applying the exponent formatting. This covers both float and Decimal inputs.

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.

1 participant