Skip to content

Commit 07efe2e

Browse files
committed
Parse toml files with tomllib/tomli
1 parent 0e6b5d2 commit 07efe2e

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ repos:
8787
exclude: ^(doc-source/conf|__pkginfo__|setup)\.(_)?py$
8888

8989
- repo: https://github.com/domdfcoding/dep_checker
90-
rev: v0.7.0
90+
rev: v0.7.1
9191
hooks:
9292
- id: dep_checker
9393
args:

pyproject_parser/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
ProjectDict,
5555
_PyProjectAsTomlDict
5656
)
57+
from pyproject_parser.utils import _load_toml
5758

5859
__author__: str = "Dominic Davis-Foster"
5960
__copyright__: str = "2021 Dominic Davis-Foster"
@@ -173,7 +174,7 @@ def load(
173174
filename = PathPlus(filename)
174175

175176
project_dir = filename.parent
176-
config = dom_toml.load(filename)
177+
config = _load_toml(filename)
177178

178179
keys = set(config.keys())
179180

pyproject_parser/__main__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
.. versionadded:: 0.2.0
88
"""
99
#
10-
# Copyright © 2021-2022 Dominic Davis-Foster <dominic@davis-foster.co.uk>
10+
# Copyright © 2021-2023 Dominic Davis-Foster <dominic@davis-foster.co.uk>
1111
#
1212
# Permission is hereby granted, free of charge, to any person obtaining a copy
1313
# of this software and associated documentation files (the "Software"), to deal
@@ -53,6 +53,7 @@
5353
# 3rd party
5454
from consolekit.terminal_colours import ColourTrilean
5555
from domdf_python_tools.typing import PathLike
56+
from toml import TomlEncoder
5657

5758
__all__ = ["main", "reformat", "check"]
5859

@@ -99,7 +100,6 @@ def check(
99100
"""
100101

101102
# 3rd party
102-
import dom_toml
103103
from dom_toml.parser import BadConfigError
104104
from domdf_python_tools.paths import PathPlus
105105
from domdf_python_tools.words import Plural, word_join
@@ -108,6 +108,7 @@ def check(
108108
from pyproject_parser import PyProject
109109
from pyproject_parser.cli import ConfigTracebackHandler, resolve_class
110110
from pyproject_parser.parsers import BuildSystemParser, PEP621Parser
111+
from pyproject_parser.utils import _load_toml
111112

112113
if not show_traceback:
113114
prettify_deprecation_warning()
@@ -120,7 +121,7 @@ def check(
120121
parser: Type[PyProject] = resolve_class(parser_class, "parser-class")
121122
parser.load(filename=pyproject_file)
122123

123-
raw_config = dom_toml.load(pyproject_file)
124+
raw_config = _load_toml(pyproject_file)
124125

125126
_keys = Plural("key", "keys")
126127

@@ -198,7 +199,6 @@ def info(
198199
import re
199200

200201
# 3rd party
201-
import dom_toml
202202
import sdjson # nodep
203203
from domdf_python_tools.paths import PathPlus, in_directory
204204

@@ -207,6 +207,7 @@ def info(
207207
from pyproject_parser.cli import _json_encoders # noqa: F401
208208
from pyproject_parser.cli import ConfigTracebackHandler, resolve_class
209209
from pyproject_parser.type_hints import Readme
210+
from pyproject_parser.utils import _load_toml
210211

211212
if not show_traceback:
212213
prettify_deprecation_warning()
@@ -229,7 +230,7 @@ def info(
229230
with in_directory(pyproject_file.parent):
230231
config.resolve_files()
231232

232-
raw_config = dom_toml.load(pyproject_file)
233+
raw_config = _load_toml(pyproject_file)
233234

234235
output: Any
235236

@@ -294,7 +295,6 @@ def reformat(
294295
from consolekit.terminal_colours import resolve_color_default # nodep
295296
from consolekit.utils import coloured_diff # nodep
296297
from domdf_python_tools.paths import PathPlus
297-
from toml import TomlEncoder
298298

299299
# this package
300300
from pyproject_parser import PyProject, _NormalisedName
@@ -309,7 +309,7 @@ def reformat(
309309

310310
with handle_tracebacks(show_traceback, ConfigTracebackHandler):
311311
parser: Type[PyProject] = resolve_class(parser_class, "parser-class")
312-
encoder: Type[TomlEncoder] = resolve_class(encoder_class, "encoder-class")
312+
encoder: Type["TomlEncoder"] = resolve_class(encoder_class, "encoder-class")
313313

314314
original_content: List[str] = pyproject_file.read_lines()
315315

pyproject_parser/utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@
3131
import io
3232
import os
3333
import sys
34-
from typing import TYPE_CHECKING, Optional
34+
from typing import TYPE_CHECKING, Any, Dict, Optional
3535

3636
# 3rd party
3737
from dom_toml.parser import BadConfigError
3838
from domdf_python_tools.paths import PathPlus
3939
from domdf_python_tools.typing import PathLike
4040

41+
if sys.version_info < (3, 11):
42+
# 3rd party
43+
import tomli as tomllib
44+
else:
45+
# 3rd party
46+
import tomllib
47+
4148
if TYPE_CHECKING:
4249
# this package
4350
from pyproject_parser.type_hints import ContentTypes
@@ -173,3 +180,15 @@ class PyProjectDeprecationWarning(Warning):
173180
174181
.. versionadded:: 0.5.0
175182
"""
183+
184+
185+
def _load_toml(filename: PathLike, ) -> Dict[str, Any]:
186+
r"""
187+
Parse TOML from the given file.
188+
189+
:param filename: The filename to read from to.
190+
191+
:returns: A mapping containing the ``TOML`` data.
192+
"""
193+
194+
return tomllib.loads(PathPlus(filename).read_text())

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ natsort>=7.1.1
66
packaging>=20.9
77
shippinglabel>=1.0.0
88
toml>=0.10.2
9+
tomli>=1.2.3; python_version < "3.11"
910
typing-extensions>=3.7.4.3

tests/test_config.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# this package
2020
from pyproject_parser.parsers import BuildSystemParser, PEP621Parser, RequiredKeysConfigParser
21-
from pyproject_parser.utils import PyProjectDeprecationWarning
21+
from pyproject_parser.utils import PyProjectDeprecationWarning, _load_toml
2222

2323

2424
@pytest.mark.parametrize("set_defaults", [True, False])
@@ -40,7 +40,7 @@ def test_pep621_class_valid_config(
4040

4141
with in_directory(tmp_pathplus):
4242
config = PEP621Parser().parse(
43-
dom_toml.load(tmp_pathplus / "pyproject.toml")["project"],
43+
_load_toml(tmp_pathplus / "pyproject.toml")["project"],
4444
set_defaults=set_defaults,
4545
)
4646

@@ -61,7 +61,7 @@ def test_pep621_subclass(
6161
(tmp_pathplus / "pyproject.toml").write_clean(toml_config)
6262

6363
with in_directory(tmp_pathplus):
64-
config = ReducedPEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
64+
config = ReducedPEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
6565

6666
advanced_data_regression.check(config)
6767

@@ -82,7 +82,7 @@ def test_pep621_class_valid_config_readme(
8282
(tmp_pathplus / filename).write_text("This is the readme.")
8383

8484
with in_directory(tmp_pathplus):
85-
config = PEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
85+
config = PEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
8686

8787
advanced_data_regression.check(config)
8888

@@ -125,7 +125,7 @@ def test_pep621_class_valid_config_readme_dict(
125125
(tmp_pathplus / "README").write_text("This is the README.")
126126

127127
with in_directory(tmp_pathplus):
128-
config = PEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
128+
config = PEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
129129

130130
advanced_data_regression.check(config)
131131

@@ -213,7 +213,7 @@ def test_pep621_class_bad_config_readme(
213213
])
214214

215215
with in_directory(tmp_pathplus), pytest.raises(exception, match=expected):
216-
PEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
216+
PEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
217217

218218

219219
@pytest.mark.parametrize("filename", ["LICENSE.rst", "LICENSE.md", "LICENSE.txt", "LICENSE"])
@@ -232,7 +232,7 @@ def test_pep621_class_valid_config_license(
232232
(tmp_pathplus / filename).write_text("This is the license.")
233233

234234
with in_directory(tmp_pathplus):
235-
config = PEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
235+
config = PEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
236236

237237
advanced_data_regression.check(config)
238238

@@ -250,7 +250,7 @@ def test_pep621_class_valid_config_license_dict(
250250
])
251251

252252
with in_directory(tmp_pathplus):
253-
config = PEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
253+
config = PEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
254254

255255
advanced_data_regression.check(config)
256256

@@ -284,7 +284,7 @@ def test_pep621_class_bad_config_license(
284284
])
285285

286286
with in_directory(tmp_pathplus), pytest.raises(BadConfigError, match=expected):
287-
PEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
287+
PEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
288288

289289

290290
@pytest.mark.parametrize(
@@ -320,7 +320,7 @@ def test_pep621_class_bad_config(
320320
(tmp_pathplus / "pyproject.toml").write_clean(config)
321321

322322
with in_directory(tmp_pathplus), pytest.raises(expects, match=match):
323-
PEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
323+
PEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
324324

325325

326326
@pytest.mark.parametrize(
@@ -352,7 +352,7 @@ def test_extra_deprecation(
352352
(tmp_pathplus / "pyproject.toml").write_clean(config)
353353

354354
with in_directory(tmp_pathplus), pytest.warns(PyProjectDeprecationWarning, match=match):
355-
PEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
355+
PEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
356356

357357

358358
@pytest.mark.parametrize("filename", ["README", "README.rtf"])
@@ -367,7 +367,7 @@ def test_parse_config_readme_errors(filename: str, tmp_pathplus: PathPlus):
367367
(tmp_pathplus / filename).write_text("This is the readme.")
368368

369369
with in_directory(tmp_pathplus), pytest.raises(ValueError, match=f"Unsupported extension for '{filename}'"):
370-
PEP621Parser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["project"])
370+
PEP621Parser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["project"])
371371

372372

373373
@pytest.mark.parametrize("set_defaults", [True, False])
@@ -380,7 +380,7 @@ def test_buildsystem_parser_valid_config(
380380
):
381381
(tmp_pathplus / "pyproject.toml").write_clean(toml_config)
382382
config = BuildSystemParser().parse(
383-
dom_toml.load(tmp_pathplus / "pyproject.toml")["build-system"],
383+
_load_toml(tmp_pathplus / "pyproject.toml")["build-system"],
384384
set_defaults=set_defaults,
385385
)
386386

@@ -394,7 +394,7 @@ def test_buildsystem_parser_errors(config: str, expects: Type[Exception], match:
394394
(tmp_pathplus / "pyproject.toml").write_clean(config)
395395

396396
with in_directory(tmp_pathplus), pytest.raises(expects, match=match):
397-
BuildSystemParser().parse(dom_toml.load(tmp_pathplus / "pyproject.toml")["build-system"])
397+
BuildSystemParser().parse(_load_toml(tmp_pathplus / "pyproject.toml")["build-system"])
398398

399399

400400
def test_RequiredKeysConfigParser():

0 commit comments

Comments
 (0)