Skip to content

Commit

Permalink
fix: Construction of OutOfOrderTableProxy can cause newlines to be in…
Browse files Browse the repository at this point in the history
…serted (#347)

Fixes #343

Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming authored May 8, 2024
1 parent 990e325 commit c2611c5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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:
Expand Down

0 comments on commit c2611c5

Please sign in to comment.