From 066aafff70d34136bbbb7d3693249f848a2aa189 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Wed, 10 May 2023 19:02:39 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Migrate=20PEP=20517=20backend=20?= =?UTF-8?q?to=20`tomllib`+`tomli`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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. --- bin/pep517_backend/_backend.py | 10 +++++++--- bin/pep517_backend/_compat.py | 11 +++++++++++ docs/changelog-fragments/501.packaging.rst | 6 ++++++ pyproject.toml | 2 +- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 bin/pep517_backend/_compat.py create mode 100644 docs/changelog-fragments/501.packaging.rst diff --git a/bin/pep517_backend/_backend.py b/bin/pep517_backend/_backend.py index b4541ba45..b9117fa09 100644 --- a/bin/pep517_backend/_backend.py +++ b/bin/pep517_backend/_backend.py @@ -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, @@ -22,12 +21,17 @@ from Cython.Build.Cythonize import main as cythonize_cli_cmd +from ._compat import load_toml_from_string # 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. @@ -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 = load_toml_from_string(config_file) return config_toml['tool']['local']['cythonize'] diff --git a/bin/pep517_backend/_compat.py b/bin/pep517_backend/_compat.py new file mode 100644 index 000000000..f471d6d0f --- /dev/null +++ b/bin/pep517_backend/_compat.py @@ -0,0 +1,11 @@ +"""Cross-interpreter compatibility shims.""" + +try: + # Python 3.11+ + from tomllib import loads as load_toml_from_string # noqa: WPS433 +except ImportError: + # before Python 3.11 + from tomli import loads as load_toml_from_string # noqa: WPS433, WPS440 + + +__all__ = ('load_toml_from_string',) # noqa: WPS410 diff --git a/docs/changelog-fragments/501.packaging.rst b/docs/changelog-fragments/501.packaging.rst new file mode 100644 index 000000000..ba730fd7e --- /dev/null +++ b/docs/changelog-fragments/501.packaging.rst @@ -0,0 +1,6 @@ +The ``toml`` build time dependency has been replaced with +``tomli`` -- by :user:`webknjaz` + +The ``tomli`` distribution is only pulled in under Python +versions below 3.11. On 3.11 and higher, the standard +library module :py:mod:`tomllib` is now used instead. diff --git a/pyproject.toml b/pyproject.toml index a6c6a9fad..b6e82c4ad 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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