Skip to content
Closed
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
8 changes: 8 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ def test_key() -> None:
assert isinstance(k, Key)


def test_key_rejects_empty_iterable() -> None:
# Empty key parts would serialize as " = 1", which is not valid TOML.
with pytest.raises(ValueError, match="at least one key part"):
tomlkit.key([])
with pytest.raises(ValueError, match="at least one key part"):
tomlkit.key(())


def test_key_value() -> None:
k, i = tomlkit.key_value("foo = 12")

Expand Down
2 changes: 2 additions & 0 deletions tomlkit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ def key(k: str | Iterable[str]) -> Key:
if isinstance(k, str):
return SingleKey(k)
keys = [SingleKey(_k) for _k in k]
if not keys:
raise ValueError("key() requires at least one key part")
if len(keys) == 1:
return keys[0]
return DottedKey(keys)
Expand Down