Hatchling metadata hook to generate all extras
Actively maintained. Last release 2 days ago, commits in 21 of the last 26 weeks. A safe choice.
pip install hatchling-autoextras-hookHatchling metadata hook to generate all extras
Actively maintained. Last release 2 days ago, commits in 21 of the last 26 weeks. A safe choice.
pip install hatchling-autoextras-hookHatchling metadata hook to automatically generate an all extra that combines all optional
dependencies.
This package provides a Hatchling metadata hook that
automatically creates an all extra in your project's optional dependencies.
The all extra will contain all dependencies from all other extras, making it easy for users to
install all optional features at once.
Add this package as a build dependency in your pyproject.toml:
[build-system]
requires = ["hatchling>=1.18.0", "hatchling-autoextras-hook"]
build-backend = "hatchling.build"
Enable the hook in your pyproject.toml:
[tool.hatch.metadata.hooks.autoextras]
Important: Hatchling metadata hooks are only triggered when there is at least one dynamic field
in your project metadata. If you don't already have any dynamic fields, you can add version as a
dynamic field:
[project]
name = "your-package"
dynamic = ["version"]
[tool.hatch.version]
path = "src/your_package/__init__.py"
Then add __version__ = "x.y.z" to your __init__.py file.
Given this configuration:
[build-system]
requires = ["hatchling>=1.18.0", "hatchling-autoextras-hook"]
build-backend = "hatchling.build"
[project]
name = "my-package"
dynamic = ["version"]
[project.optional-dependencies]
dev = ["pytest>=7.0", "black>=22.0"]
docs = ["sphinx>=5.0", "sphinx-rtd-theme>=1.0"]
typing = ["mypy>=1.0"]
[tool.hatch.version]
path = "src/my_package/__init__.py"
[tool.hatch.metadata.hooks.autoextras]
The hook will automatically generate an all extra combining all dependencies:
[project.optional-dependencies]
all = [
"black>=22.0",
"mypy>=1.0",
"pytest>=7.0",
"sphinx-rtd-theme>=1.0",
"sphinx>=5.0",
]
# ... dev, docs, typing remain unchanged
Users can then install all optional dependencies with:
pip install your-package[all]
These are the guarantees of the hook:
| Property | Statement |
|---|---|
| Union of the extras | The generated extra contains every dependency string of every non-excluded extra, and nothing else. It never contains its own previous content. The strings are copied, not interpreted (see the self-references below). If there is nothing to collect, the generated extra is empty, so that pkg[all] stays installable. |
| Deterministic | The output is the list of the distinct stripped dependency strings, sorted as strings (code point order, so Zed comes before alpha), whatever the order of the extras. The generated extra is always the last key of the table, whether it is new, replaced or renamed by overwrite. |
| Additive | Original extras are never modified, reordered or removed. Only the generated extra is added (or replaced when overwrite = true). |
| Atomic | The hook either completes or raises with the metadata unchanged. |
| Idempotent on request | With overwrite = true, running update again on its own output gives the same metadata. With overwrite = false the second run raises, because the extra exists. |
| Name-normalized | Extra names are compared after the normalization of PEP 685 (Dev_Extra, dev-extra and dev.extra are the same extra). |
| Strict | By default, an unknown option, a wrong type, an invalid name, a non-string or blank dependency, and an exclude entry that matches no extra raise. |
| Lenient on request | With HATCHLING_AUTOEXTRAS_WARN_ONLY=1, the unknown options and the unmatched exclude entries warn. Every other problem still raises. |
| Output-invariant | The warn-only mode never changes the generated extra. A build that succeeds in both modes produces the same metadata: the mode only turns an error into a warning for a problem that has no effect on the result. |
| Early and named errors | Configuration errors are raised before any metadata is read. Every message of a configuration problem names the option and the table [tool.hatch.metadata.hooks.autoextras]. Metadata errors name the extra and the position of the faulty entry. |
Pure besides metadata | update reads the validated options and mutates only metadata. No file access, no global state, no state kept between calls. The only environment variable read is HATCHLING_AUTOEXTRAS_WARN_ONLY, once, when the hook is created. |
| Minimal | One runtime dependency (hatchling). One hook class, one entry point, __version__. Three options, all keyword-style entries of the pyproject.toml hook table. |
| Observable | What was generated is logged at DEBUG level. |
Not guaranteed:
numpy>=2 and numpy >= 2 are two strings, so they are two entries of
the generated extra. Hatchling merges them when it normalizes the metadata.docs = ["pkg[dev]"] is a string like any other. With exclude = ["dev"],
the generated extra contains pkg[dev] (Hatchling before 1.23) or the dependencies of dev
(Hatchling 1.23 and later, which resolves the reference): excluding an extra does not remove it
from an extra that depends on it.allow-ambiguous-features.project.dynamic is not empty (see
How Hatchling Calls the Hook). The hook cannot detect that it was
not called.All options are optional. Enabling the hook with an empty table is enough:
[tool.hatch.metadata.hooks.autoextras]
| Option | Type | Default | Description |
|---|---|---|---|
extra-name | string | "all" | Name of the generated extra. Must be a valid extra name. |
exclude | list[str] | [] | Names of extras that must not be included in the generated extra. |
overwrite | boolean | false | Replace an existing extra with the same name instead of raising. |
Example:
[tool.hatch.metadata.hooks.autoextras]
extra-name = "complete"
exclude = ["dev", "docs"]
overwrite = false
With this configuration, pip install my-package[complete] installs the dependencies of every
extra except dev and docs.
The guarantees that hold whatever the options are listed in Properties.
extra-nameName of the extra that the hook generates (default: "all"). Users install it with
pip install my-package[<extra-name>]. The value must be a valid extra name (letters, digits, -,
_ and ., starting and ending with a letter or a digit, no whitespace), otherwise a ValueError
is raised (a TypeError if it is not a string). Names are compared after the normalization of
PEP 685, like Hatchling does: Full_Extra, full-extra and
full.extra are the same extra.
[tool.hatch.metadata.hooks.autoextras]
extra-name = "complete"
[project.optional-dependencies]
dev = ["pytest>=7.0"]
docs = ["sphinx>=5.0"]
complete = ["pytest>=7.0", "sphinx>=5.0"] # generated
Use it when all is already taken by a hand-written extra, or when you prefer another name such as
full or complete. If an extra with this name already exists, see overwrite.
excludeList of extra names to leave out of the generated extra (default: []). The excluded extras remain
available on their own; only their dependencies are not copied into the generated extra.
[tool.hatch.metadata.hooks.autoextras]
exclude = ["dev", "docs"]
[project.optional-dependencies]
dev = ["pytest>=7.0"]
docs = ["sphinx>=5.0"]
aws = ["boto3>=1.0"]
all = ["boto3>=1.0"] # generated: dev and docs are excluded
Notes:
exclude = ["B.c"] excludes B_c, and
exclude = ["A"] excludes a (see the matching rules in the Properties).ValueError).ValueError (a typo would otherwise put the dependencies
of an extra you wanted to exclude into the generated extra). With
HATCHLING_AUTOEXTRAS_WARN_ONLY=1 it emits a UserWarning and has no effect.TypeError is raised.overwriteBoolean (default: false) controlling what happens when an extra named extra-name already exists
in [project.optional-dependencies] (the names are compared normalized, so All conflicts with all).
false: the build fails with a ValueError, so a hand-written extra is never lost silently.true: the existing extra is replaced by the generated one. Its previous content is not
merged: it is ignored when collecting dependencies and discarded. The generated extra is
always the last key of optional-dependencies, whatever the spelling of the replaced extra.[project.optional-dependencies]
dev = ["pytest>=7.0"]
all = ["numpy"] # hand-written
[tool.hatch.metadata.hooks.autoextras]
overwrite = true
# result: all = ["pytest>=7.0"]
The value must be a boolean (true/false, not a string), otherwise a TypeError is raised.
The build fails with an explicit message in the following cases:
| Situation | Exception | Warn-only mode |
|---|---|---|
| The hook table contains an unknown option | ValueError | warning |
exclude has a name that matches no extra | ValueError | warning |
An extra named extra-name already exists and overwrite = false | ValueError | error |
extra-name is not a valid extra name | ValueError | error |
extra-name is not a string | TypeError | error |
exclude contains the generated extra | ValueError | error |
exclude is not a list of strings | TypeError | error |
overwrite is not a boolean | TypeError | error |
optional-dependencies is not a table, or an extra is not a list | TypeError | error |
| A dependency of an extra is not a string | TypeError | error |
| A dependency of an extra is blank | ValueError | error |
HATCHLING_AUTOEXTRAS_WARN_ONLY has an unrecognized value | ValueError | error |
Unknown options (for example excludes instead of exclude) and exclude entries that match no
extra fail the build, because a typo would otherwise be ignored silently. A person who builds a
project they do not control can downgrade these two problems to a UserWarning with an
environment variable, which is read when the hook is created:
HATCHLING_AUTOEXTRAS_WARN_ONLY=1 python -m build
Accepted values (case-insensitive): 1, true, yes, on enable the warn-only mode; 0,
false, no, off, an empty value or an unset variable keep the strict mode. Any other value
raises a ValueError. The unknown option and the unmatched exclude entry are ignored in the
warn-only mode. Every other problem still raises.
[tool.hatch.metadata.hooks.autoextras]
extra-name = "full"
exclude = ["dev", "test"]
overwrite = true
project.dynamic is not empty. Without it, the hook is
not called and no extra is generated, without any message.dynamic = ["optional-dependencies"] cannot be combined with a static
[project.optional-dependencies] table: Hatchling fails with Metadata field optional-dependencies cannot be both statically defined and listed in field project.dynamic.dynamic = ["optional-dependencies"] without a static table: the hook runs without extras and
generates all = [].After setting up the hook, check the result with hatch project metadata optional-dependencies, or
build the project (python -m build) and look for Provides-Extra: all in the METADATA of the
wheel.
The hook can also be used directly, which is mostly useful for testing:
from hatchling_autoextras_hook import AutoExtrasMetadataHook
metadata = {"optional-dependencies": {"dev": ["pytest>=7.0", "black>=22.0"]}}
hook = AutoExtrasMetadataHook("root", {"extra-name": "complete"})
hook.update(metadata) # modifies `metadata` in place
Problem: The generated extra is not present.
Solutions:
Ensure you have at least one dynamic field in your [project] section:
[project]
dynamic = ["version"]
Hatchling metadata hooks only run when there are dynamic fields.
Verify the hook is listed in your [build-system]:
[build-system]
requires = ["hatchling>=1.18.0", "hatchling-autoextras-hook"]
Check that the hook configuration exists:
[tool.hatch.metadata.hooks.autoextras]
Cannot create the 'all' extra: 'all' already existsProblem: The build raises a ValueError because an all extra is already defined (the message
names the existing extra, whatever its spelling: All, a_ll, ...).
Solution: Set overwrite = true, choose another extra-name, or remove your manual all
extra.
Problem: Some dependencies are missing.
Solutions: Ensure the extras are defined in [project.optional-dependencies] (the hook does not
read [dependency-groups]), and check that they are not listed in exclude.
Problem: The build fails with errors related to the hook.
Solutions:
pyproject.toml syntax is correctpython -m build -vall extra?A: No, not by default. The build fails with a ValueError so nothing is silently lost. Set
overwrite = true to replace it, or use a different extra-name.
A: A typo in an option name (excludes) or in an exclude entry (tset) would otherwise be
ignored silently, and the dependencies of an extra you meant to exclude (often dev or test) would
end up in the public generated extra. The hook fails and names the culprit. To downgrade the unknown
options and the unmatched exclude entries to warnings, set HATCHLING_AUTOEXTRAS_WARN_ONLY=1 in
the environment of the build (see Strict and warn-only modes).
all?A: Yes, use the exclude option.
A: No. The hook only works with [project.optional-dependencies]. Dependency groups in
[dependency-groups] are not part of the package metadata.
A: No, the hook only collects, deduplicates and sorts dependency strings.
A: No, the hook is specific to Hatchling.
A: Build your package and inspect the wheel metadata:
python -m build
unzip -p dist/your_package-*.whl '*/METADATA' | grep "Provides-Extra: all"
A: No, it only modifies the package metadata during the build. pyproject.toml is unchanged.
This project uses uv for dependency management. See CONTRIBUTING.md
for setup instructions (make setup-venv) and dev/README.md for development
scripts.
hatchling-autoextras-hook | hatchling | python |
|---|---|---|
main | >=1.18,<2.0 | >=3.11 |
0.1.3 | >=1.18,<2.0 | >=3.10 |
0.1.2 | >=1.18,<2.0 | >=3.10 |
0.1.1 | >=1.18,<2.0 | >=3.10 |
0.1.0 | >=1.18,<2.0 | >=3.10 |
0.0.2 | >=1.18,<2.0 | >=3.10,<3.15 |
0.0.1 | >=1.18,<2.0 | >=3.10,<3.15 |
Please check the instructions in CONTRIBUTING.md.
Everyone is welcome to contribute to the community. If you have any questions or suggestions, you can submit Github Issues. We will reply to you as soon as possible. Thank you very much.
:warning: While hatchling-autoextras-hook is in development stage, no API is guaranteed to be
stable from one release to the next.
In fact, it is very likely that the API will change multiple times before a stable 1.0.0 release.
In practice, this means that upgrading hatchling-autoextras-hook to a new version will possibly
break any code that was using the old version of hatchling-autoextras-hook.
For information about security policies and how to report vulnerabilities, please see our Security Policy.
hatchling-autoextras-hook is licensed under BSD 3-Clause "New" or "Revised" license available
in LICENSE file.
Use together with hatchling-autoextras-hook: extensions, drivers, servers and companion tools.