Skip to content

Commit c2611c5

Browse files
authored
fix: Construction of OutOfOrderTableProxy can cause newlines to be inserted (#347)
Fixes #343 Signed-off-by: Frost Ming <[email protected]>
1 parent 990e325 commit c2611c5

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Remove the extra minus sign added to the float value after calculation. ([#341](https://github.com/python-poetry/tomlkit/issues/341))
8+
- Fix unexpected newline added after accessing the out-of-order table. ([#343](https://github.com/python-poetry/tomlkit/issues/343))
89

910
## [0.12.4] - 2024-02-27
1011

tests/test_items.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,3 +969,30 @@ def encode_decimal(obj):
969969

970970
assert api.dumps({"foo": decimal.Decimal("1.23")}) == "foo = 1.23\n"
971971
api.unregister_encoder(encode_decimal)
972+
973+
974+
def test_no_extra_minus_sign():
975+
doc = parse("a = -1")
976+
assert doc.as_string() == "a = -1"
977+
doc["a"] *= -1
978+
assert doc.as_string() == "a = +1"
979+
doc["a"] *= -1
980+
assert doc.as_string() == "a = -1"
981+
982+
doc = parse("a = -1.5")
983+
assert doc.as_string() == "a = -1.5"
984+
doc["a"] *= -1
985+
assert doc.as_string() == "a = +1.5"
986+
doc["a"] *= -1
987+
assert doc.as_string() == "a = -1.5"
988+
989+
990+
def test_no_newline_after_visit_oo_table():
991+
content = """\
992+
[a.b.c.d]
993+
[unrelated]
994+
[a.b.e]
995+
"""
996+
doc = parse(content)
997+
doc["a"]
998+
assert doc.as_string() == content

tomlkit/container.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,8 @@ def __init__(self, container: Container, indices: tuple[int]) -> None:
794794
self._tables = []
795795
self._tables_map = {}
796796

797+
original_parsing = container._parsed
798+
container.parsing(True)
797799
for i in indices:
798800
_, item = self._container._body[i]
799801

@@ -805,7 +807,7 @@ def __init__(self, container: Container, indices: tuple[int]) -> None:
805807
self._tables_map[k] = table_idx
806808
if k is not None:
807809
dict.__setitem__(self, k.key, v)
808-
810+
container.parsing(original_parsing)
809811
self._internal_container._validate_out_of_order_table()
810812

811813
def unwrap(self) -> str:

0 commit comments

Comments
 (0)