From db4db1bf23fcedbdb6572ee98ea38f4af9a44165 Mon Sep 17 00:00:00 2001 From: AryanRG Date: Sat, 17 Oct 2026 11:05:30 +0530 Subject: [PATCH] Preserve table-key history when copying containers --- tests/test_toml_document.py | 20 ++++++++++++++++++++ tomlkit/container.py | 1 + 2 files changed, 21 insertions(+) diff --git a/tests/test_toml_document.py b/tests/test_toml_document.py index 84b114b9..12de22fc 100644 --- a/tests/test_toml_document.py +++ b/tests/test_toml_document.py @@ -486,6 +486,26 @@ def test_toml_document_can_be_copied() -> None: assert json.loads(json.dumps(doc)) == {"foo": {"bar": 1}} +@pytest.mark.parametrize("copy_document", [copy.copy, TOMLDocument.copy]) +@pytest.mark.parametrize("intervening", ["", "[other]\nz = 3\n"]) +def test_copied_document_can_extend_super_table( + copy_document: Callable[[TOMLDocument], TOMLDocument], intervening: str +) -> None: + source = "[a.b]\nx = 1\n" + intervening + doc = copy_document(parse(source)) + extension = tomlkit.table(True) + extension.add("c", {"y": 2}) + + doc.add("a", extension) + + expected: dict[str, Any] = {"a": {"b": {"x": 1}, "c": {"y": 2}}} + if intervening: + expected["other"] = {"z": 3} + assert doc.unwrap() == expected + assert doc.as_string() == source + "\n[a.c]\ny = 2\n" + assert parse(doc.as_string()).unwrap() == expected + + def test_getting_inline_table_is_still_an_inline_table() -> None: content = """\ [tool.poetry] diff --git a/tomlkit/container.py b/tomlkit/container.py index 9265c740..11eb4b72 100644 --- a/tomlkit/container.py +++ b/tomlkit/container.py @@ -1102,6 +1102,7 @@ def __copy__(self) -> Self: c._body += self.body c._map.update(self._map) + c._table_keys += self._table_keys c._out_of_order_keys |= self._out_of_order_keys return c