From ffa1ec4ef8c1f6fbba798c7c6d66ac1935997d48 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Wed, 8 May 2024 16:56:47 +0800 Subject: [PATCH] fix: Construction of OutOfOrderTableProxy can cause newlines to be inserted Fixes #343 Signed-off-by: Frost Ming --- CHANGELOG.md | 1 + tests/test_items.py | 27 +++++++++++++++++++++++++++ tomlkit/container.py | 4 +++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19eed13..c178509 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixed - Remove the extra minus sign added to the float value after calculation. ([#341](https://github.com/python-poetry/tomlkit/issues/341)) +- Fix unexpected newline added after accessing the out-of-order table. ([#343](https://github.com/python-poetry/tomlkit/issues/343)) ## [0.12.4] - 2024-02-27 diff --git a/tests/test_items.py b/tests/test_items.py index ca75966..1a71b04 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -969,3 +969,30 @@ def encode_decimal(obj): assert api.dumps({"foo": decimal.Decimal("1.23")}) == "foo = 1.23\n" api.unregister_encoder(encode_decimal) + + +def test_no_extra_minus_sign(): + doc = parse("a = -1") + assert doc.as_string() == "a = -1" + doc["a"] *= -1 + assert doc.as_string() == "a = +1" + doc["a"] *= -1 + assert doc.as_string() == "a = -1" + + doc = parse("a = -1.5") + assert doc.as_string() == "a = -1.5" + doc["a"] *= -1 + assert doc.as_string() == "a = +1.5" + doc["a"] *= -1 + assert doc.as_string() == "a = -1.5" + + +def test_no_newline_after_visit_oo_table(): + content = """\ +[a.b.c.d] +[unrelated] +[a.b.e] +""" + doc = parse(content) + doc["a"] + assert doc.as_string() == content diff --git a/tomlkit/container.py b/tomlkit/container.py index ecac3f1..06e6483 100644 --- a/tomlkit/container.py +++ b/tomlkit/container.py @@ -794,6 +794,8 @@ def __init__(self, container: Container, indices: tuple[int]) -> None: self._tables = [] self._tables_map = {} + original_parsing = container._parsed + container.parsing(True) for i in indices: _, item = self._container._body[i] @@ -805,7 +807,7 @@ def __init__(self, container: Container, indices: tuple[int]) -> None: self._tables_map[k] = table_idx if k is not None: dict.__setitem__(self, k.key, v) - + container.parsing(original_parsing) self._internal_container._validate_out_of_order_table() def unwrap(self) -> str: