Skip to content

Commit f645dcb

Browse files
webknjazQalthos
authored andcommitted
🎨 Migrate PEP 517 backend to tomllib+tomli
`tomllib` is a part of stdlib since Python 3.11 and `tomli` is used as a fallback for the older Python versions. The latter is API-compatible with the former.
1 parent 61bd3aa commit f645dcb

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

bin/pep517_backend/_backend.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from functools import wraps
99
from pathlib import Path
1010

11-
import toml
1211
from expandvars import expandvars
1312
from setuptools.build_meta import ( # noqa: F401 # Re-exporting PEP 517 hooks
1413
build_sdist, build_wheel, get_requires_for_build_sdist,
@@ -22,12 +21,17 @@
2221

2322
from Cython.Build.Cythonize import main as cythonize_cli_cmd
2423

24+
from ._compat import tomllib # noqa: WPS436
2525
from ._transformers import ( # noqa: WPS436
2626
convert_to_kwargs_only, get_cli_kwargs_from_config,
2727
get_enabled_cli_flags_from_config,
2828
)
2929

3030

31+
PROJECT_ROOT_DIR = Path(__file__).parents[2].resolve()
32+
PYPROJECT_TOML_PATH = PROJECT_ROOT_DIR / 'pyproject.toml'
33+
34+
3135
def get_config():
3236
"""Grab optional build dependencies from pyproject.toml config.
3337
@@ -80,8 +84,8 @@ def get_config():
8084
# This section can contain cythonize options
8185
# NAME = "VALUE"
8286
"""
83-
config_file = (Path.cwd().resolve() / 'pyproject.toml').read_text()
84-
config_toml = toml.loads(config_file)
87+
config_file = PYPROJECT_TOML_PATH.read_text(encoding='utf-8')
88+
config_toml = tomllib.loads(config_file)
8589
return config_toml['tool']['local']['cythonize']
8690

8791

bin/pep517_backend/_compat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Cross-interpreter compatibility shims."""
2+
3+
try:
4+
import tomllib # Python 3.11+ # noqa: WPS433
5+
except ImportError:
6+
import tomli as tomllib # before Python 3.11 # noqa: WPS433, WPS440
7+
8+
9+
__all__ = ('tomllib',) # noqa: WPS410

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = [
33
# Essentials
44
"Cython", # needed by in-tree build backend `bin/pep517_backend.py`
55
"setuptools>=45", # needed by in-tree build backend `bin/pep517_backend.py`
6-
"toml", # needed by in-tree build backend `bin/pep517_backend.py`
6+
"tomli; python_version < '3.11'", # needed by in-tree build backend `bin/pep517_backend.py`
77
"expandvars", # needed by in-tree build backend for env vars interpolation
88

99
# Plugins

0 commit comments

Comments
 (0)