Skip to content

Commit 0c45a51

Browse files
committed
Fix: accept ints as keys (interpret them as strings)
1 parent 1a3085c commit 0c45a51

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

tests/test_toml_document.py

Lines changed: 5 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,7 @@ def test_overwrite_out_of_order_table_key():
11871188
11881189
"""
11891190
)
1191+
1192+
1193+
def test_set_default_int():
1194+
TOMLDocument().setdefault(4, 5)

tomlkit/items.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,15 @@ class SingleKey(Key):
375375

376376
def __init__(
377377
self,
378-
k: str,
378+
k: str | int,
379379
t: KeyType | None = None,
380380
sep: str | None = None,
381381
original: str | None = None,
382382
) -> None:
383+
# keys are allowed to be ints but should be interpreted as strings
384+
if not isinstance(k, str):
385+
k = str(k)
386+
383387
if t is None:
384388
if not k or any(
385389
c not in string.ascii_letters + string.digits + "-" + "_" for c in k

0 commit comments

Comments
 (0)