From 2a71cb9a6e8db17876c69d08281ca9dee6d4d184 Mon Sep 17 00:00:00 2001 From: Jean Abou Samra Date: Tue, 14 Nov 2023 20:34:25 +0100 Subject: [PATCH 1/5] Expand pyproject.toml guide to also cover the [build-system] table --- source/guides/writing-pyproject-toml.rst | 103 +++++++++++++++++++---- 1 file changed, 87 insertions(+), 16 deletions(-) diff --git a/source/guides/writing-pyproject-toml.rst b/source/guides/writing-pyproject-toml.rst index 3c3c741c99..0e1eeef55e 100644 --- a/source/guides/writing-pyproject-toml.rst +++ b/source/guides/writing-pyproject-toml.rst @@ -4,27 +4,98 @@ Writing your ``pyproject.toml`` =============================== -``pyproject.toml`` is a configuration file used by packaging tools. Most -:term:`build backends ` [#poetry-special]_ allow you to specify -your project's basic metadata, such as the dependencies, your name, etc., -in the ``[project]`` table of your ``pyproject.toml``. +``pyproject.toml`` is a configuration file used by packaging tools, as +well as other tools such as linters, type checkers, etc. There are +three possible TOML tables in this file. + +- The ``[build-system]`` table is **strongly recommended**. It allows + you to declare which :term:`build backend` you use and which other + dependencies are needed to build your project. + +- The ``[project]`` table is the format that most build backends use to specify + your project's basic metadata, such as the dependencies, your name, etc. + +- The ``[tool]`` table has arbitrary subtables corresponding to tools, whether + packaging-related or not, e.g., ``[tool.hatch]``, ``[tool.black]``, + ``[tool.mypy]``. We only touch upon this table here because its contents are + entirely tool-specific. Consult each tool's documentation to know what it can + contain. .. note:: - You may have heard of ``setup.py`` and ``setup.cfg`` for the setuptools_ - build backend. For new projects, it is recommended to use ``pyproject.toml`` - for basic metadata, and keep ``setup.py`` only if some programmatic configuration - is needed (especially building C extensions). However, putting basic project - metadata in ``setup.py`` or ``setup.cfg`` is still valid. See + There is a significant difference between the ``[build-system]`` and + ``[project]`` tables. The former should always be present, regardless of + which build backend you use (since it *defines* the tool you use). The latter + is understood by *most* build backends, but some build backends use a + different format. + + At the time of this writing (November 2023), Poetry_ is a notable build + backend that does not use the ``[project]`` table (it uses the + ``[tool.poetry]`` table instead). + + Also, the setuptools_ build backend supports both the ``[project]`` table, + and the older format in ``setup.cfg`` or ``setup.py``. For new projects, it + is recommended to use the ``[project]`` table, and keep ``setup.py`` only if + some programmatic configuration is needed (especially building C extensions), + but the ``setup.cfg`` and ``setup.py`` formats are still valid. See :ref:`setup-py-deprecated`. + +Declaring the build backend +=========================== + +The ``[build-system]`` table contains a ``build-backend`` key, which specifies +the build backend to be used. It also contains a ``requires`` key, which is a +list of dependencies needed to build the project -- this is typically just the +build backend package, but it may also contain additional dependencies. You can +also constrain the versions, e.g., ``requires = ["setuptools >= 61.0"]``. + +Usually, you'll just copy what your build backend's documentation suggests. Here +are the values for some common build backends: + +.. tab:: Hatchling + + .. code-block:: toml + + [build-system] + requires = ["hatchling"] + build-backend = "hatchling.build" + +.. tab:: setuptools + + .. code-block:: toml + + [build-system] + requires = ["setuptools>=61.0"] + build-backend = "setuptools.build_meta" + +.. tab:: Flit + + .. code-block:: toml + + [build-system] + requires = ["flit_core>=3.4"] + build-backend = "flit_core.buildapi" + +.. tab:: PDM + + .. code-block:: toml + + [build-system] + requires = ["pdm-backend"] + build-backend = "pdm.backend" + + +The rest of this guide is devoted to the ``[project]`` table. + + Static vs. dynamic metadata =========================== -Most of the time, you will directly write the value of a field in -``pyproject.toml``. For example: ``requires-python = ">= 3.8"``, or -``version = "1.0"``. +Most of the time, you will directly write the value of a ``[project]`` +field. For example: ``requires-python = ">= 3.8"``, or ``version = +"1.0"``. However, in some cases, it is useful to let your build backend compute the metadata for you. For example: many build backends can read the @@ -316,6 +387,10 @@ A full example .. code-block:: toml + [build-system] + requires = ["hatchling"] + build-backend = "hatchling.build" + [project] name = "spam-eggs" version = "2020.0.0" @@ -370,10 +445,6 @@ A full example ------------------ -.. [#poetry-special] At the time of this writing (November 2023), Poetry_ - is a notable exception. It uses its own format for this metadata, in - the ``[tool.poetry]`` table. - .. [#requires-python-upper-bounds] Think twice before applying an upper bound like ``requires-python = "<= 3.10"`` here. `This blog post `_ contains some information regarding possible problems. From 7607fab1a42d3027206da4eec9ec5c37beef6058 Mon Sep 17 00:00:00 2001 From: Jean Abou-Samra Date: Tue, 14 Nov 2023 21:29:28 +0100 Subject: [PATCH 2/5] Spaces around >= Co-authored-by: chrysle --- source/guides/writing-pyproject-toml.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/guides/writing-pyproject-toml.rst b/source/guides/writing-pyproject-toml.rst index 0e1eeef55e..6ec8a9d8ce 100644 --- a/source/guides/writing-pyproject-toml.rst +++ b/source/guides/writing-pyproject-toml.rst @@ -67,7 +67,7 @@ are the values for some common build backends: .. code-block:: toml [build-system] - requires = ["setuptools>=61.0"] + requires = ["setuptools >= 61.0"] build-backend = "setuptools.build_meta" .. tab:: Flit @@ -75,7 +75,7 @@ are the values for some common build backends: .. code-block:: toml [build-system] - requires = ["flit_core>=3.4"] + requires = ["flit_core >= 3.4"] build-backend = "flit_core.buildapi" .. tab:: PDM From 26d3fb9ed056deb62e912c720eb8b864fe0244b6 Mon Sep 17 00:00:00 2001 From: Jean Abou Samra Date: Tue, 14 Nov 2023 21:37:24 +0100 Subject: [PATCH 3/5] Link to "Choosing a build backend" --- source/guides/writing-pyproject-toml.rst | 5 +++-- source/tutorials/packaging-projects.rst | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/source/guides/writing-pyproject-toml.rst b/source/guides/writing-pyproject-toml.rst index 6ec8a9d8ce..ae2bdbc776 100644 --- a/source/guides/writing-pyproject-toml.rst +++ b/source/guides/writing-pyproject-toml.rst @@ -51,8 +51,9 @@ list of dependencies needed to build the project -- this is typically just the build backend package, but it may also contain additional dependencies. You can also constrain the versions, e.g., ``requires = ["setuptools >= 61.0"]``. -Usually, you'll just copy what your build backend's documentation suggests. Here -are the values for some common build backends: +Usually, you'll just copy what your build backend's documentation +suggests (after :ref:`choosing your build backend `). +Here are the values for some common build backends: .. tab:: Hatchling diff --git a/source/tutorials/packaging-projects.rst b/source/tutorials/packaging-projects.rst index 2678c97b9b..1bc2f77ba7 100644 --- a/source/tutorials/packaging-projects.rst +++ b/source/tutorials/packaging-projects.rst @@ -103,6 +103,8 @@ Creating a test directory :file:`tests/` is a placeholder for test files. Leave it empty for now. +.. _choosing-build-backend: + Choosing a build backend ------------------------ From d80aea34f16b560d12a7d1d5847a7d7fde14703d Mon Sep 17 00:00:00 2001 From: Jean Abou Samra Date: Wed, 15 Nov 2023 12:22:28 +0100 Subject: [PATCH 4/5] Address @willingc's review --- source/guides/writing-pyproject-toml.rst | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/source/guides/writing-pyproject-toml.rst b/source/guides/writing-pyproject-toml.rst index ae2bdbc776..12fadd7304 100644 --- a/source/guides/writing-pyproject-toml.rst +++ b/source/guides/writing-pyproject-toml.rst @@ -15,11 +15,10 @@ three possible TOML tables in this file. - The ``[project]`` table is the format that most build backends use to specify your project's basic metadata, such as the dependencies, your name, etc. -- The ``[tool]`` table has arbitrary subtables corresponding to tools, whether - packaging-related or not, e.g., ``[tool.hatch]``, ``[tool.black]``, - ``[tool.mypy]``. We only touch upon this table here because its contents are - entirely tool-specific. Consult each tool's documentation to know what it can - contain. +- The ``[tool]`` table has tool-specific subtables, e.g., ``[tool.hatch]``, + ``[tool.black]``, ``[tool.mypy]``. We only touch upon this table here because + its contents are defined by each tool. Consult the particular tool's + documentation to know what it can contain. .. note:: @@ -36,7 +35,7 @@ three possible TOML tables in this file. Also, the setuptools_ build backend supports both the ``[project]`` table, and the older format in ``setup.cfg`` or ``setup.py``. For new projects, it is recommended to use the ``[project]`` table, and keep ``setup.py`` only if - some programmatic configuration is needed (especially building C extensions), + some programmatic configuration is needed (such as building C extensions), but the ``setup.cfg`` and ``setup.py`` formats are still valid. See :ref:`setup-py-deprecated`. @@ -88,12 +87,12 @@ Here are the values for some common build backends: build-backend = "pdm.backend" -The rest of this guide is devoted to the ``[project]`` table. - Static vs. dynamic metadata =========================== +The rest of this guide is devoted to the ``[project]`` table. + Most of the time, you will directly write the value of a ``[project]`` field. For example: ``requires-python = ">= 3.8"``, or ``version = "1.0"``. From 344da81ccacd0c7d7d12eb565db90e9ef968ec71 Mon Sep 17 00:00:00 2001 From: Jean Abou-Samra Date: Thu, 16 Nov 2023 17:18:54 +0100 Subject: [PATCH 5/5] Be less formal Co-authored-by: chrysle --- source/guides/writing-pyproject-toml.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/guides/writing-pyproject-toml.rst b/source/guides/writing-pyproject-toml.rst index 12fadd7304..e0f02ddf29 100644 --- a/source/guides/writing-pyproject-toml.rst +++ b/source/guides/writing-pyproject-toml.rst @@ -28,7 +28,7 @@ three possible TOML tables in this file. is understood by *most* build backends, but some build backends use a different format. - At the time of this writing (November 2023), Poetry_ is a notable build + At the time of writing this (November 2023), Poetry_ is a notable build backend that does not use the ``[project]`` table (it uses the ``[tool.poetry]`` table instead).