Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed

- Fix invalid output after deleting the last key under a dotted key inside an inline table; the emptied table now renders as `b = {}`. ([#664](https://github.com/python-poetry/tomlkit/pull/664))
- Fix valid TOML being rejected when an out-of-order table reopens an implicit super-table with a dotted key: `[a.b.c]` then `[a]` with `b.z = 1` extends the implicit `a.b` super-table rather than redefining it (stdlib `tomllib` accepts it), so it now parses and round-trips instead of raising `Redefinition of an existing table`. The concrete-table case (`[a.b]` then `[a]` `b.z = 1`) stays rejected. ([#565](https://github.com/python-poetry/tomlkit/pull/565))
- Fix appending a key to a parsed inline table that uses `", "` separators emitting a bare `","` for the new entry, leaving the table inconsistently spaced (e.g. `{x = 1, y = 2,z = 3}`). ([#595](https://github.com/python-poetry/tomlkit/pull/595))

Expand Down
27 changes: 27 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,33 @@ def test_adding_to_nested_dotted_key_inside_inline_table() -> None:
assert parse(rendered).as_string() == rendered


@pytest.mark.parametrize(
("content", "expected"),
[
("a = {x = 1, b.c = 1, y = 2}\n", "a = {x = 1, b = {}, y = 2}\n"),
("a = {x = 1, b.c = 1}\n", "a = {x = 1, b = {}}\n"),
("a = {b.c = 1, y = 2}\n", "a = {b = {}, y = 2}\n"),
("a = {b.c = 1}\n", "a = {b = {}}\n"),
],
)
def test_emptying_dotted_key_inside_inline_table(content: str, expected: str) -> None:
doc = parse(content)
del doc["a"]["b"]["c"]

# With nothing left under the ``b.`` prefix, the table must still render
# as a key, not as an empty slot between two commas.
assert doc.as_string() == expected
assert parse(expected) == doc


def test_emptying_nested_dotted_key_inside_inline_table() -> None:
doc = parse("a = {x = 1, b.c.d = 1, y = 2}\n")
del doc["a"]["b"]["c"]["d"]

assert doc.as_string() == "a = {x = 1, b.c = {}, y = 2}\n"
assert parse(doc.as_string()) == doc


def test_appending_to_comma_first_array_does_not_double_separator() -> None:
doc = parse(
"""\
Expand Down
3 changes: 3 additions & 0 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,9 @@ def _render_dotted(self, key: Key, table: Table) -> list[str]:
f"{prefix}{v.trivia.indent}{k.as_string()}{k.sep}{v.as_string()}"
f"{v.trivia.comment_ws}{v.trivia.comment}{trail}"
)
if not parts:
# every key under the prefix was removed, keep it as an empty table
return [f"{key.as_string()} = {{}}"]
return parts

def __setitem__(self, key: Key | str, value: Any) -> None:
Expand Down
Loading