Metadata-Version: 2.5
Name: hatchling-autoextras-hook
Version: 0.2.0
Dynamic: Maintainer
Dynamic: Maintainer-email
Summary: Hatchling metadata hook to generate all extras
Project-URL: Homepage, https://github.com/durandtibo/hatchling-autoextras-hook
Project-URL: Repository, https://github.com/durandtibo/hatchling-autoextras-hook
Project-URL: Changelog, https://github.com/durandtibo/hatchling-autoextras-hook/releases
Project-URL: Issues, https://github.com/durandtibo/hatchling-autoextras-hook/issues
Author-email: Thibaut Durand <durand.tibo+gh@gmail.com>
License-Expression: BSD-3-Clause
License-File: LICENSE
Keywords: automation,hatchling,hook,metadata,optional-dependencies
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: MacOS
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: hatchling<2.0,>=1.18
Description-Content-Type: text/markdown

# hatchling-autoextras-hook

<!-- markdownlint-disable MD013 -->
<p align="center">
    <a href="https://github.com/durandtibo/hatchling-autoextras-hook/actions/workflows/ci.yaml"><img alt="CI" src="https://img.shields.io/github/actions/workflow/status/durandtibo/hatchling-autoextras-hook/ci.yaml?branch=main&label=CI&logo=github"></a>
    <a href="https://github.com/durandtibo/hatchling-autoextras-hook/actions/workflows/nightly-package.yaml"><img alt="Nightly Package Tests" src="https://img.shields.io/github/actions/workflow/status/durandtibo/hatchling-autoextras-hook/nightly-package.yaml?label=nightly&logo=github"></a>
    <a href="https://codecov.io/gh/durandtibo/hatchling-autoextras-hook"><img alt="Codecov" src="https://img.shields.io/codecov/c/github/durandtibo/hatchling-autoextras-hook?logo=codecov"></a>
    <a href="https://durandtibo.github.io/hatchling-autoextras-hook/"><img alt="Documentation (stable)" src="https://img.shields.io/badge/docs-stable-blue?logo=readthedocs&logoColor=white"></a>
    <br/>
    <a href="https://pypi.org/project/hatchling-autoextras-hook/"><img alt="PyPI version" src="https://img.shields.io/pypi/v/hatchling-autoextras-hook?logo=pypi&logoColor=white"></a>
    <a href="https://pypi.org/project/hatchling-autoextras-hook/"><img alt="Python" src="https://img.shields.io/pypi/pyversions/hatchling-autoextras-hook?logo=python&logoColor=white"></a>
    <a href="https://opensource.org/licenses/BSD-3-Clause"><img alt="BSD-3-Clause" src="https://img.shields.io/pypi/l/hatchling-autoextras-hook"></a>
    <br/>
    <a href="https://github.com/astral-sh/ruff"><img alt="Ruff" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json"></a>
    <a href="https://google.github.io/styleguide/pyguide.html#s3.8-comments-and-docstrings"><img alt="Doc style: google" src="https://img.shields.io/badge/docstyle-google-3666d6"></a>
    <a href="https://scorecard.dev/viewer/?uri=github.com/durandtibo/hatchling-autoextras-hook"><img alt="OpenSSF Scorecard" src="https://img.shields.io/ossf-scorecard/github.com/durandtibo/hatchling-autoextras-hook?label=openssf%20scorecard"></a>
    <br/>
    <a href="https://pepy.tech/project/hatchling-autoextras-hook"><img alt="Downloads" src="https://img.shields.io/pepy/dt/hatchling-autoextras-hook"></a>
    <a href="https://pepy.tech/project/hatchling-autoextras-hook"><img alt="Monthly downloads" src="https://img.shields.io/pepy/dm/hatchling-autoextras-hook"></a>
    <a href="https://github.com/durandtibo/hatchling-autoextras-hook/stargazers"><img alt="GitHub stars" src="https://img.shields.io/github/stars/durandtibo/hatchling-autoextras-hook?logo=github"></a>
</p>
<!-- markdownlint-enable MD013 -->

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

## Overview

This package provides a [Hatchling](https://pypi.org/project/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`:

```toml
[build-system]
requires = ["hatchling>=1.18.0", "hatchling-autoextras-hook"]
build-backend = "hatchling.build"
```

## Usage

Enable the hook in your `pyproject.toml`:

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

```toml
[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:

```toml
[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:

```toml
[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:

```bash
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](#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:

```toml
[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:

```toml
[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](#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](https://peps.python.org/pep-0685/), like Hatchling does: `Full_Extra`, `full-extra` and
`full.extra` are the same extra.

```toml
[tool.hatch.metadata.hooks.autoextras]
extra-name = "complete"
```

```toml
[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`](#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.

```toml
[tool.hatch.metadata.hooks.autoextras]
exclude = ["dev", "docs"]
```

```toml
[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](#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.

```toml
[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:

```bash
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

```toml
[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:

```python
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:

   ```toml
   [project]
   dynamic = ["version"]
   ```

   Hatchling metadata hooks only run when there are dynamic fields.

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

   ```toml
   [build-system]
   requires = ["hatchling>=1.18.0", "hatchling-autoextras-hook"]
   ```

3. Check that the hook configuration exists:

   ```toml
   [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](#strict-and-warn-only-modes)).

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

**A:** Yes, use the [`exclude`](#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:

```bash
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](.github/CONTRIBUTING.md)
for setup instructions (`make setup-venv`) and [dev/README.md](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](.github/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](https://github.com/durandtibo/hatchling-autoextras-hook/issues).
We will reply to you as soon as possible. Thank you very much.

## API stability

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

## Security

For information about security policies and how to report vulnerabilities, please see our
[Security Policy](SECURITY.md).

## License

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