diff --git a/tests/test_toml_document.py b/tests/test_toml_document.py index 84b114b9..1cfa51c6 100644 --- a/tests/test_toml_document.py +++ b/tests/test_toml_document.py @@ -664,6 +664,77 @@ def test_valid_out_of_order_independent_tables() -> None: assert doc.as_string() == "[a]\nx=1\n[zz]\n[a.b]\nc=1\n" +def test_valid_nested_out_of_order_table_after_unrelated_table() -> None: + content = "[a]\n[a.b.c]\n[a.b]\n[[zz]]\n[a.b.d]\n" + doc = parse(content) + assert doc.unwrap() == {"a": {"b": {"c": {}, "d": {}}}, "zz": [{}]} + assert doc.as_string() == content + + +def test_valid_nested_out_of_order_table_with_multiple_fragments() -> None: + content = "[a]\n[a.b.c]\n[a.b]\nx=1\n[a.b.d.e]\n[[zz]]\n[a.b.f]\n" + doc = parse(content) + assert doc.unwrap() == { + "a": {"b": {"c": {}, "x": 1, "d": {"e": {}}, "f": {}}}, + "zz": [{}], + } + assert doc.as_string() == content + + +@pytest.mark.parametrize( + "content", + [ + pytest.param( + "[a.b.c]\n[a.b]\n[a]\n[a.b.d]\n[a.b]\n", + id="duplicate-concrete-table-across-fragments", + ), + pytest.param( + "[a]\n[a.b.c]\n[a.b]\n[q]\n[a.b.d]\n[a.b]\n", + id="duplicate-concrete-table-in-earlier-fragment", + ), + pytest.param( + "[a]\n[q]\n[a.b.c.e]\n[a.b]\n[a]\n[a.b.d]\n", + id="duplicate-concrete-parent-in-earlier-fragment", + ), + pytest.param( + "a.x=1\n[a.b.c]\n[a.b.d]\n[q]\n[a.b.d]\n", + id="duplicate-table-merged-into-validated-fragment", + ), + pytest.param( + "[a]\nb.y=1\n[a.b.c]\n[q]\n[a.b]\n", + id="dotted-key-table-redefined-by-header", + ), + ], +) +def test_out_of_order_table_rejects_redefinition(content: str) -> None: + with pytest.raises(TOMLKitError): + parse(content) + + +def test_valid_out_of_order_sub_table_of_dotted_key_table() -> None: + content = "[a]\nb.y=1\n[a.b.c]\n[q]\n[a.b.d]\n" + doc = parse(content) + assert doc.unwrap() == {"a": {"b": {"y": 1, "c": {}, "d": {}}}, "q": {}} + assert doc.as_string() == content + + +@pytest.mark.parametrize( + "header", + [ + pytest.param("[a.b]", id="duplicate-concrete-parent"), + pytest.param("[a.b.c]", id="duplicate-child-in-first-fragment"), + pytest.param("[a.b.d.e]", id="duplicate-nested-child-in-later-fragment"), + pytest.param("[a.b.x]", id="scalar-redefined-as-table"), + ], +) +def test_nested_out_of_order_table_rejects_collisions(header: str) -> None: + prefix = "[a]\n[a.b.c]\n[a.b]\nx=1\n[a.b.d.e]\n[[zz]]\n" + # The existing fragments are valid; only the appended header collides. + assert parse(prefix).as_string() == prefix + with pytest.raises(ParseError): + parse(prefix + header + "\n") + + def test_valid_out_of_order_super_table_extended_by_dotted_key() -> None: # A deep header [a.b.c] makes a.b an implicit super-table; reopening [a] and # extending a.b via a dotted key (b.z) only adds a key to that super-table, diff --git a/tomlkit/container.py b/tomlkit/container.py index 9265c740..dd5e8fd8 100644 --- a/tomlkit/container.py +++ b/tomlkit/container.py @@ -375,6 +375,7 @@ def append( return self elif current.is_super_table(): + self._validate_earlier_fragments(current_idx, item) if item.is_super_table(): # We need to merge both super tables if ( @@ -408,6 +409,24 @@ def append( # Mutating in place is O(1) per merge. The defensive copy # that protected the out-of-order validation pass has been # moved into OutOfOrderTableProxy (its only consumer). + cached = self._validation_cache.get(key) + if ( + cached is not None + and isinstance(current_idx, tuple) + and cached[0] == len(current_idx) + ): + # `current` is already merged into the cached + # validation container of this out-of-order key; + # merge the new part there too, or later fragments + # would be validated against a stale copy. + temp = cached[1] + try: + for k, v in item.value.body: + temp.append(k, copy.deepcopy(v), validate=True) + temp._validate_out_of_order_table() + except Exception: + self._validation_cache.pop(key, None) + raise for k, v in item.value.body: current.append(k, v) @@ -491,27 +510,57 @@ def append( self._validate_out_of_order_table(key) return self + def _validate_earlier_fragments( + self, idx: int | tuple[int, ...], candidate: Table + ) -> None: + # `append` compares a concrete `[table]` with the last fragment of an + # out-of-order table only; it must not redefine an earlier concrete or + # dotted-key fragment either, e.g. [a] [q] [a.b.c] [a] declares [a] + # twice. Nested collisions are caught by the incremental validation. + if candidate.is_super_table() or not isinstance(idx, tuple): + return + for i in idx[:-1]: + k, fragment = self._body[i] + if isinstance(fragment, Table) and ( + not fragment.is_super_table() or (k is not None and k.is_dotted()) + ): + raise KeyAlreadyPresent(k) + def _validate_table_candidate(self, current: Table, candidate: Table) -> None: for k, v in candidate.value.body: if k is None: continue if k in current.value._map: - existing = current.value.item(k) - if isinstance(existing, (Table, AoT)) != isinstance(v, (Table, AoT)): - raise KeyAlreadyPresent(k) - if k.is_dotted() and not ( - isinstance(existing, Table) and existing.is_super_table() - ): - raise TOMLKitError("Redefinition of an existing table") - if isinstance(existing, Table) and isinstance(v, Table): - if not existing.is_super_table() and not v.is_super_table(): - # Both sides are concrete `[table]` definitions of the - # same name; the table is declared twice. + index = current.value._map[k] + indices = index if isinstance(index, tuple) else (index,) + for i in indices: + existing_key, existing = current.value._body[i] + if isinstance(existing, (Table, AoT)) != isinstance( + v, (Table, AoT) + ): raise KeyAlreadyPresent(k) - # One side is still an implicit/super table, so a duplicate - # (if any) is nested deeper - keep checking the subtree. - self._validate_table_candidate(existing, v) + if k.is_dotted() and not ( + isinstance(existing, Table) and existing.is_super_table() + ): + raise TOMLKitError("Redefinition of an existing table") + if ( + existing_key is not None + and existing_key.is_dotted() + and isinstance(v, Table) + and not v.is_super_table() + ): + # A table created by dotted keys (stored as a super + # table) cannot be redefined by a `[table]` header. + raise TOMLKitError("Redefinition of an existing table") + if isinstance(existing, Table) and isinstance(v, Table): + if not existing.is_super_table() and not v.is_super_table(): + # Both sides are concrete `[table]` definitions of the + # same name; the table is declared twice. + raise KeyAlreadyPresent(k) + # One side is still an implicit/super table, so a duplicate + # (if any) is nested deeper - keep checking the subtree. + self._validate_table_candidate(existing, v) continue if not k.is_dotted():