Skip to content
Open
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
15 changes: 15 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ def test_parser_rejects_tab_in_bare_key(content: str) -> None:
parser = Parser(content)
with pytest.raises(ParseError):
parser.parse()


@pytest.mark.parametrize(
"content",
[
"[a.b]\n[a]\n[a.b]",
"[a.b]\nx = 1\n[a]\n[a.b]\ny = 2",
"[a.b.c]\n[a]\n[a.b.c]",
"[a.b]\n[a.c]\n[a]\n[a.b]",
],
)
def test_parser_rejects_table_redefined_after_parent(content: str) -> None:
parser = Parser(content)
with pytest.raises(ParseError):
parser.parse()
8 changes: 8 additions & 0 deletions tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ def _validate_table_candidate(self, current: Table, candidate: Table) -> None:
raise KeyAlreadyPresent(k)
if k.is_dotted():
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():
Expand Down
Loading