Skip to content

Commit e44e075

Browse files
feat: replace toml with tomli/tomli-w for TOML encode/decode
Agent-Logs-Url: https://github.com/fabiocaccamo/python-benedict/sessions/89c27897-48ad-4322-bc90-768b03047974 Co-authored-by: fabiocaccamo <1035294+fabiocaccamo@users.noreply.github.com>
1 parent 253e40d commit e44e075

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

benedict/serializers/toml.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
try:
2-
import toml
3-
4-
toml_installed = True
5-
except ModuleNotFoundError:
6-
toml_installed = False
7-
81
try:
92
# python >= 3.11
103
import tomllib
@@ -13,6 +6,20 @@
136
except ImportError:
147
tomllib_available = False
158

9+
try:
10+
import tomli
11+
12+
tomli_installed = True
13+
except ModuleNotFoundError:
14+
tomli_installed = False
15+
16+
try:
17+
import tomli_w
18+
19+
tomli_w_installed = True
20+
except ModuleNotFoundError:
21+
tomli_w_installed = False
22+
1623
from typing import Any
1724

1825
from benedict.extras import require_toml
@@ -35,11 +42,11 @@ def decode(self, s: str, **kwargs: Any) -> Any:
3542
if tomllib_available:
3643
data = tomllib.loads(s, **kwargs)
3744
else:
38-
require_toml(installed=toml_installed)
39-
data = toml.loads(s, **kwargs)
45+
require_toml(installed=tomli_installed)
46+
data = tomli.loads(s, **kwargs)
4047
return data
4148

4249
def encode(self, d: Any, **kwargs: Any) -> str:
43-
require_toml(installed=toml_installed)
44-
data = toml.dumps(dict(d), **kwargs)
50+
require_toml(installed=tomli_w_installed)
51+
data = tomli_w.dumps(dict(d), **kwargs)
4552
return data

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ s3 = [
138138
"boto3 >= 1.24.89, < 2.0.0",
139139
]
140140
toml = [
141-
"toml >= 0.10.2, < 1.0.0",
141+
"tomli >= 2.0.0, < 3.0.0; python_version < '3.11'",
142+
"tomli-w >= 1.0.0, < 2.0.0",
142143
]
143144
xls = [
144145
"openpyxl >= 3.0.0, < 4.0.0",

tests/dicts/io/test_io_dict_toml.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def test_from_toml_with_valid_data(self) -> None:
5050

5151
@unittest.skipIf(
5252
tomllib_available,
53-
"standard tomlib is available, exception will not be raised",
53+
"standard tomllib is available, exception will not be raised",
5454
)
55-
@patch("benedict.serializers.toml.toml_installed", False)
55+
@patch("benedict.serializers.toml.tomli_installed", False)
5656
def test_from_toml_with_valid_data_but_toml_extra_not_installed(self) -> None:
5757
j = """
5858
a = 1
@@ -184,7 +184,7 @@ def test_to_toml_file(self) -> None:
184184
self.assertFileExists(filepath)
185185
self.assertEqual(d, IODict.from_toml(filepath))
186186

187-
@patch("benedict.serializers.toml.toml_installed", False)
187+
@patch("benedict.serializers.toml.tomli_w_installed", False)
188188
def test_to_toml_with_extra_not_installed(self) -> None:
189189
d = IODict(
190190
{

0 commit comments

Comments
 (0)