Skip to content

Commit 5627e7c

Browse files
committed
Add .api.decimal() encoder
Simply casts to `Decimal` then calls `item()` on it as normal for most basic types.
1 parent 6869269 commit 5627e7c

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

tomlkit/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from tomlkit.api import comment
66
from tomlkit.api import date
77
from tomlkit.api import datetime
8+
from tomlkit.api import decimal
89
from tomlkit.api import document
910
from tomlkit.api import dump
1011
from tomlkit.api import dumps
@@ -33,6 +34,7 @@
3334
"comment",
3435
"date",
3536
"datetime",
37+
"decimal",
3638
"document",
3739
"dump",
3840
"dumps",

tomlkit/api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime as _datetime
44

55
from collections.abc import Mapping
6+
from decimal import Decimal
67
from typing import IO
78
from typing import Iterable
89

@@ -99,6 +100,10 @@ def float_(raw: str | float) -> Float:
99100
"""Create an float item from a number or string."""
100101
return item(float(raw))
101102

103+
def decimal(raw: str | Decimal | float) -> String:
104+
"""Create an string item from a ``decimal.Decimal``."""
105+
return item(Decimal(raw))
106+
102107

103108
def boolean(raw: str) -> Bool:
104109
"""Turn `true` or `false` into a boolean item."""

0 commit comments

Comments
 (0)