Skip to content

Commit dde2d57

Browse files
committed
Use pyproject.toml with hatchling instead of setuptools
The main motivation for this change is to migrate from the non-standard `setup.py` to the standard PEP 517 `setup.py`. The secondary motivation is to use a build backend that's easier to use and harder to get wrong than setuptools. Hatchling is the most popular build backend behind setuptools (I can provide data if required), and much easier to use than setuptools. Notably, it will include `py.typed` and `.pyi` files automatically. This also applies to most other build backends. This means we can remove the setuptools-specific documentation. packaging.python.org has a tab system to show different build backends (https://packaging.python.org/en/latest/guides/writing-pyproject-toml/). Afaik typing.python.org doesn't, so we only show one build backend. Fixes #2121
1 parent 211b0cb commit dde2d57

File tree

1 file changed

+22
-49
lines changed

1 file changed

+22
-49
lines changed

docs/guides/libraries.rst

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,25 @@ A typical directory structure would look like:
8282

8383
.. code-block:: text
8484
85-
setup.py
85+
pyproject.toml
8686
my_great_package/
8787
__init__.py
8888
stuff.py
8989
py.typed
9090
9191
It's important to ensure that the ``py.typed`` marker file is included in the
92-
distributed package. If using ``setuptools``, this can be achieved like so:
92+
distributed package. If using ``hatchling``, it is included by default:
9393

94-
.. code-block:: python
94+
.. code-block:: toml
9595
96-
from setuptools import setup
96+
[project]
97+
name = "my-great-package"
98+
version = "0.1.0"
99+
requires-python = ">=3.14"
97100
98-
setup(
99-
name="my_great_distribution",
100-
version="0.1",
101-
package_data={"my_great_package": ["py.typed"]},
102-
packages=["my_great_package"],
103-
)
101+
[build-system]
102+
requires = ["hatchling"]
103+
build-backend = "hatchling.build"
104104
105105
106106
Type stub files included in the package
@@ -113,27 +113,13 @@ directory structure would look like:
113113

114114
.. code-block:: text
115115
116-
setup.py
116+
pyproject.toml
117117
my_great_package/
118118
__init__.py
119119
stuff.py
120120
stuff.pyi
121121
py.typed
122122
123-
If using ``setuptools``, we can ensure the ``.pyi`` and ``py.typed`` files are
124-
included like so:
125-
126-
.. code-block:: python
127-
128-
from setuptools import setup
129-
130-
setup(
131-
name="my_great_distribution",
132-
version="0.1",
133-
package_data={"my_great_package": ["py.typed", "stuff.pyi"]},
134-
packages=["my_great_package"],
135-
)
136-
137123
The presence of ``.pyi`` files does not affect the Python interpreter at runtime
138124
in any way. However, static type checkers will only look at the ``.pyi`` file and
139125
ignore the corresponding ``.py`` file.
@@ -150,41 +136,28 @@ For example:
150136

151137
.. code-block:: text
152138
153-
setup.py
139+
pyproject.toml
154140
my_great_package-stubs/
155141
__init__.pyi
156142
stuff.pyi
157143
144+
.. code-block:: toml
158145
159-
.. code-block:: python
160-
161-
from setuptools import setup
146+
[project]
147+
name = "my-great-package-stubs"
148+
version = "0.1.0"
149+
requires-python = ">=3.14"
162150
163-
setup(
164-
name="my_great_package-stubs",
165-
version="0.1",
166-
package_data={"my_great_package-stubs": ["__init__.pyi", "stuff.pyi"]},
167-
packages=["my_great_package-stubs"]
168-
)
151+
[build-system]
152+
requires = ["hatchling"]
153+
build-backend = "hatchling.build"
169154
155+
[tool.hatch.build.targets.wheel]
156+
packages = ["src/my_great_package-stubs"]
170157
171158
Users are then able to install the stubs-only package separately to provide
172159
types for the original library.
173160

174-
Inclusion in sdist
175-
^^^^^^^^^^^^^^^^^^
176-
177-
Note that to ensure inclusion of ``.pyi`` and ``py.typed`` files in an sdist
178-
(.tar.gz archive), you may also need to modify the inclusion rules in your
179-
``MANIFEST.in`` (see the
180-
`packaging guide <https://packaging.python.org/en/latest/guides/using-manifest-in/>`__
181-
for more details on ``MANIFEST.in``). For example:
182-
183-
.. code-block:: text
184-
185-
global-include *.pyi
186-
global-include py.typed
187-
188161
.. _type_completeness:
189162

190163
How much of my library needs types?

0 commit comments

Comments
 (0)