-
Notifications
You must be signed in to change notification settings - Fork 282
Use pyproject.toml with hatchling instead of setuptools
#2138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
konstin
wants to merge
2
commits into
python:main
Choose a base branch
from
konstin:konsti/hatchling-instead-of-setuptools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,25 +82,15 @@ A typical directory structure would look like: | |
|
|
||
| .. code-block:: text | ||
|
|
||
| setup.py | ||
| pyproject.toml | ||
| my_great_package/ | ||
| __init__.py | ||
| stuff.py | ||
| py.typed | ||
|
|
||
| It's important to ensure that the ``py.typed`` marker file is included in the | ||
| distributed package. If using ``setuptools``, this can be achieved like so: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from setuptools import setup | ||
|
|
||
| setup( | ||
| name="my_great_distribution", | ||
| version="0.1", | ||
| package_data={"my_great_package": ["py.typed"]}, | ||
| packages=["my_great_package"], | ||
| ) | ||
| distributed package. Modern build backends such as ``hatchling`` include it | ||
| by default. | ||
|
|
||
|
|
||
| Type stub files included in the package | ||
|
|
@@ -113,27 +103,13 @@ directory structure would look like: | |
|
|
||
| .. code-block:: text | ||
|
|
||
| setup.py | ||
| pyproject.toml | ||
| my_great_package/ | ||
| __init__.py | ||
| stuff.py | ||
| stuff.pyi | ||
| py.typed | ||
|
|
||
| If using ``setuptools``, we can ensure the ``.pyi`` and ``py.typed`` files are | ||
| included like so: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from setuptools import setup | ||
|
|
||
| setup( | ||
| name="my_great_distribution", | ||
| version="0.1", | ||
| package_data={"my_great_package": ["py.typed", "stuff.pyi"]}, | ||
| packages=["my_great_package"], | ||
| ) | ||
|
|
||
| The presence of ``.pyi`` files does not affect the Python interpreter at runtime | ||
| in any way. However, static type checkers will only look at the ``.pyi`` file and | ||
| ignore the corresponding ``.py`` file. | ||
|
|
@@ -150,41 +126,28 @@ For example: | |
|
|
||
| .. code-block:: text | ||
|
|
||
| setup.py | ||
| pyproject.toml | ||
| my_great_package-stubs/ | ||
| __init__.pyi | ||
| stuff.pyi | ||
|
|
||
| .. code-block:: toml | ||
|
|
||
| .. code-block:: python | ||
| [project] | ||
| name = "my-great-package-stubs" | ||
| version = "0.1.0" | ||
| requires-python = ">=3.14" | ||
|
|
||
| from setuptools import setup | ||
|
|
||
| setup( | ||
| name="my_great_package-stubs", | ||
| version="0.1", | ||
| package_data={"my_great_package-stubs": ["__init__.pyi", "stuff.pyi"]}, | ||
| packages=["my_great_package-stubs"] | ||
| ) | ||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["src/my_great_package-stubs"] | ||
|
Comment on lines
+145
to
+146
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uv-build supports those without extra configuration, but hatchling is wider used. |
||
|
|
||
| Users are then able to install the stubs-only package separately to provide | ||
| types for the original library. | ||
|
|
||
| Inclusion in sdist | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Note that to ensure inclusion of ``.pyi`` and ``py.typed`` files in an sdist | ||
| (.tar.gz archive), you may also need to modify the inclusion rules in your | ||
| ``MANIFEST.in`` (see the | ||
| `packaging guide <https://packaging.python.org/en/latest/guides/using-manifest-in/>`__ | ||
| for more details on ``MANIFEST.in``). For example: | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| global-include *.pyi | ||
| global-include py.typed | ||
|
|
||
| .. _type_completeness: | ||
|
|
||
| How much of my library needs types? | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This note is afaik only relevant for setuptools anymore. Since the other build backends I'm aware support this in the default configuration, I've shortened it to this hint, the specifics fit better in the setuptools documentation.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather make a general statement, instead of focusing on hatchling. Something like "Most modern build tools like
hatchlinginclude it by default."Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setuptools>=69includes this by default too, so I think we can just remove this statement / I think we can just not mention build backends in this guideThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shrunk the note to mentioning it needs to be included and all modern package managers do it by default, i.e. don't put it in your gitignore where hatchling skips it.