Skip to content

Commit df4afc6

Browse files
authored
Fix: reject single keys that aren't strings (#416)
* Fix: accept ints as keys (interpret them as strings) * fix: throw type error instead of casting to str
1 parent 832e855 commit df4afc6

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

tests/test_toml_document.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from tomlkit._utils import _utc
1616
from tomlkit.api import document
1717
from tomlkit.exceptions import NonExistentKey
18+
from tomlkit.toml_document import TOMLDocument
1819

1920

2021
def test_document_is_a_dict(example):
@@ -1187,3 +1188,8 @@ def test_overwrite_out_of_order_table_key():
11871188
11881189
"""
11891190
)
1191+
1192+
1193+
def test_set_default_int():
1194+
with pytest.raises(TypeError):
1195+
TOMLDocument().setdefault(4, 5)

tomlkit/items.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ def __init__(
380380
sep: str | None = None,
381381
original: str | None = None,
382382
) -> None:
383+
if not isinstance(k, str):
384+
raise TypeError("Keys must be strings")
385+
383386
if t is None:
384387
if not k or any(
385388
c not in string.ascii_letters + string.digits + "-" + "_" for c in k

0 commit comments

Comments
 (0)