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/packaging/pep517_backend/_backend.py b/packaging/pep517_backend/_backend.py index b4541ba45..b9117fa09 100644 --- a/packaging/pep517_backend/_backend.py +++ b/packaging/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/packaging/pep517_backend/_compat.py b/packaging/pep517_backend/_compat.py new file mode 100644 index 000000000..f471d6d0f --- /dev/null +++ b/packaging/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/pyproject.toml b/pyproject.toml index 999841cb6..a3e18aca9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = [ # Essentials "Cython", # needed by in-tree build backend `packaging/pep517_backend.py` "setuptools>=45", # needed by in-tree build backend `packaging/pep517_backend.py` - "toml", # needed by in-tree build backend `packaging/pep517_backend.py` + "tomli; python_version < '3.11'", # needed by in-tree build backend `packaging/pep517_backend.py` "expandvars", # needed by in-tree build backend for env vars interpolation # Plugins