From a62fe9194df6dc87b101c83e321b484b4af25faa Mon Sep 17 00:00:00 2001 From: Andrew Stellman Date: Fri, 2 Oct 2026 18:24:54 -0400 Subject: [PATCH] Do not add a plus sign to a negative zero float result Float._new adds "+" when the source had an explicit sign and the result is >= 0. -0.0 >= 0 is True, so "a = -1.5" times 0 was written as "a = +-0.0", which tomlkit's own parser rejects. Add the "+" only when str(result) does not already start with "-". Found by Quality Playbook, an AI code-review tool, with Claude; I reviewed the change. --- tests/test_items.py | 13 +++++++++++++ tomlkit/items.py | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_items.py b/tests/test_items.py index cf6134aa..e814b5f6 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -1451,6 +1451,19 @@ def test_no_extra_minus_sign() -> None: assert doc.as_string() == "a = -1.5" +def test_no_extra_plus_sign_on_negative_zero() -> None: + doc = parse("a = -1.5") + doc["a"] *= 0 + assert doc.as_string() == "a = -0.0" + doc["a"] *= -1 + assert doc.as_string() == "a = +0.0" + + # Integer true division also goes through Float._new. + doc = parse("a = +0") + doc["a"] /= -1 + assert doc.as_string() == "a = -0.0" + + def test_serialize_table_with_dotted_key() -> None: child = api.table() child.add(api.key(("b", "c")), 1) diff --git a/tomlkit/items.py b/tomlkit/items.py index 8589df97..db151f7b 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -876,7 +876,8 @@ def as_string(self) -> str: def _new(self, result: float) -> Float: raw = str(result) - if self._sign and result >= 0: + # -0.0 >= 0 is True, but str(-0.0) already carries its sign. + if self._sign and result >= 0 and not raw.startswith("-"): raw = f"+{raw}" return Float(result, self._trivia, raw)