Skip to content

Commit

Permalink
🎨 Migrate PEP 517 backend to tomllib+tomli
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
webknjaz authored and Qalthos committed Nov 10, 2023
1 parent e8c9e78 commit 46f244e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 7 additions & 3 deletions bin/pep517_backend/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from functools import wraps
from pathlib import Path

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

from Cython.Build.Cythonize import main as cythonize_cli_cmd

from ._compat import tomllib # noqa: WPS436
from ._transformers import ( # noqa: WPS436
convert_to_kwargs_only, get_cli_kwargs_from_config,
get_enabled_cli_flags_from_config,
)


PROJECT_ROOT_DIR = Path(__file__).parents[2].resolve()
PYPROJECT_TOML_PATH = PROJECT_ROOT_DIR / 'pyproject.toml'


def get_config():
"""Grab optional build dependencies from pyproject.toml config.
Expand Down Expand Up @@ -80,8 +84,8 @@ def get_config():
# This section can contain cythonize options
# NAME = "VALUE"
"""
config_file = (Path.cwd().resolve() / 'pyproject.toml').read_text()
config_toml = toml.loads(config_file)
config_file = PYPROJECT_TOML_PATH.read_text(encoding='utf-8')
config_toml = tomllib.loads(config_file)
return config_toml['tool']['local']['cythonize']


Expand Down
9 changes: 9 additions & 0 deletions bin/pep517_backend/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Cross-interpreter compatibility shims."""

try:
import tomllib # Python 3.11+ # noqa: WPS433
except ImportError:
import tomli as tomllib # before Python 3.11 # noqa: WPS433, WPS440


__all__ = ('tomllib',) # noqa: WPS410
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = [
# Essentials
"Cython", # needed by in-tree build backend `bin/pep517_backend.py`
"setuptools>=45", # needed by in-tree build backend `bin/pep517_backend.py`
"toml", # needed by in-tree build backend `bin/pep517_backend.py`
"tomli; python_version < '3.11'", # needed by in-tree build backend `bin/pep517_backend.py`
"expandvars", # needed by in-tree build backend for env vars interpolation

# Plugins
Expand Down

0 comments on commit 46f244e

Please sign in to comment.