Skip to main content

hatchling-autoextras-hook

CI Nightly Package Tests Codecov Documentation (stable)
PyPI version Python BSD-3-Clause
Ruff Doc style: google OpenSSF Scorecard
Downloads Monthly downloads GitHub stars

Hatchling metadata hook to automatically generate an all extra that combines all optional dependencies.

Overview

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.

Installation

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"

Usage

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.

Example

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]

Features

  • Automatically combines all optional dependencies into a single extra
  • Removes duplicates across extras and strips whitespace
  • Configurable extra name, exclusion list, and overwrite behavior
  • Compatible with all Hatchling build targets (wheel, sdist)

Properties

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:

  • Validity of a requirement. The hook does not parse requirements. A string that is not a valid PEP 508 requirement is copied as is and rejected by Hatchling afterwards.
  • Semantic deduplication. 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.
  • Self-references. 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.
  • PEP 735 dependency groups and the Hatchling option allow-ambiguous-features.
  • Runs of the hook. Hatchling runs a metadata hook only when project.dynamic is not empty (see How Hatchling Calls the Hook). The hook cannot detect that it was not called.

Configuration

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.

Option details

extra-name

Name 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.

exclude

List 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:

  • Names are compared after the normalization of PEP 685: exclude = ["B.c"] excludes B_c, and exclude = ["A"] excludes a (see the matching rules in the Properties).
  • The generated extra cannot be excluded (ValueError).
  • A name that matches no extra raises a 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.
  • A dependency that also appears in a non-excluded extra is still included.
  • The value must be a list of strings, otherwise a TypeError is raised.

overwrite

Boolean (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.

Errors

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

Strict and warn-only modes

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.

Advanced Usage

Combining Options

[tool.hatch.metadata.hooks.autoextras]
extra-name = "full"
exclude = ["dev", "test"]
overwrite = true

How Hatchling Calls the Hook

  • Hatchling runs a metadata hook only when 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 = [].
  • The interaction with other metadata hooks is not specified.

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.

Python API

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

Troubleshooting

Hook Not Triggering

Problem: The generated extra is not present.

Solutions:

  1. 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.

  2. Verify the hook is listed in your [build-system]:

    [build-system]
    requires = ["hatchling>=1.18.0", "hatchling-autoextras-hook"]
    
  3. Check that the hook configuration exists:

    [tool.hatch.metadata.hooks.autoextras]
    

Cannot create the 'all' extra: 'all' already exists

Problem: 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.

Dependencies Missing from the Generated 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.

Build Failures

Problem: The build fails with errors related to the hook.

Solutions:

  1. Verify you are using Hatchling >= 1.18.0
  2. Check the error message: it identifies the invalid option or extra
  3. Check that your pyproject.toml syntax is correct
  4. Build with verbose output: python -m build -v

FAQ

Q: Will this hook override my manually defined all 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.

Q: Why does the build fail on a typo?

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).

Q: Can I exclude specific extras from being included in all?

A: Yes, use the exclude option.

Q: Does this work with dependency groups (PEP 735)?

A: No. The hook only works with [project.optional-dependencies]. Dependency groups in [dependency-groups] are not part of the package metadata.

Q: Will this slow down my build process?

A: No, the hook only collects, deduplicates and sorts dependency strings.

Q: Can I use this with Poetry or PDM?

A: No, the hook is specific to Hatchling.

Q: How do I verify the hook is working?

A: Build your package and inspect the wheel metadata:

python -m build
unzip -p dist/your_package-*.whl '*/METADATA' | grep "Provides-Extra: all"

Q: Does this hook modify my source files?

A: No, it only modifies the package metadata during the build. pyproject.toml is unchanged.

Development

This project uses uv for dependency management. See CONTRIBUTING.md for setup instructions (make setup-venv) and dev/README.md for development scripts.

Dependencies

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

Contributing

Please check the instructions in CONTRIBUTING.md.

Suggestions and Communication

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.

API stability

⚠️ 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.

Security

For information about security policies and how to report vulnerabilities, please see our Security Policy.

License

hatchling-autoextras-hook is licensed under BSD 3-Clause "New" or "Revised" license available in LICENSE file.

Metadata

Release files for hatchling-autoextras-hook 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hatchling-autoextras-hook 0.2.0
File Size Uploaded Provenance Actions
hatchling_autoextras_hook-0.2.0.tar.gz 32.4 kB With provenance Details for hatchling_autoextras_hook-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hatchling-autoextras-hook 0.2.0
File Interpreter ABI Platform Actions
hatchling_autoextras_hook-0.2.0-py3-none-any.whl Python 3 none any Details for hatchling_autoextras_hook-0.2.0-py3-none-any.whl

Total release size: 48.3 kB

Release files / hatchling_autoextras_hook-0.2.0.tar.gz

Download URL hatchling_autoextras_hook-0.2.0.tar.gz
Size 32.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b2aa212ba3006fb8c87c36c212aca79081267ca916f26bdb9d466cd5742764ea
BLAKE2b-256 checksum
How to use checksums
6ae6255fa82869e6d200ad68e5693342e59999addf13fcbc2e43268517353e45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / hatchling_autoextras_hook-0.2.0-py3-none-any.whl

Download URL hatchling_autoextras_hook-0.2.0-py3-none-any.whl
Size 15.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b43a71efe19d08ba9b4e1bdd8070fe298de224d70cec9f2d9d31429d6354c1af
BLAKE2b-256 checksum
How to use checksums
881ccd7436341e6faa1af223c7a48536bc5fc3c59a301cbda1fa0fdc7e75e7d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for hatchling-autoextras-hook 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for hatchling_autoextras_hook-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page