Metadata-Version: 2.5
Name: reqexpand
Version: 0.1.0
Summary: Expand package extras without dropping PEP 508 environment markers.
Project-URL: Homepage, https://github.com/driftwright/reqexpand
Project-URL: Issues, https://github.com/driftwright/reqexpand/issues
Project-URL: Changelog, https://github.com/driftwright/reqexpand/blob/main/CHANGELOG.md
Author: driftwright
License-Expression: MIT
License-File: LICENSE
Keywords: extras,markers,packaging,pep508,requirements
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Hatch
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: packaging>=23
Requires-Dist: tomli>=2; python_version < '3.11'
Provides-Extra: dev
Requires-Dist: build>=1; extra == 'dev'
Requires-Dist: hatchling>=1.18; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff==0.17.6; extra == 'dev'
Requires-Dist: tomli>=2; extra == 'dev'
Description-Content-Type: text/markdown

# reqexpand

Expand a local project's extras into concrete requirements without dropping
PEP 508 environment markers.

A requirement such as `my-app[win]; sys_platform == "win32"` often loses that
marker when a tool substitutes the `win` extra's dependencies. reqexpand
replaces extras of the named project with those dependencies and combines
the parent marker with each dependency's own marker using `and`. Third-party
extras such as `coverage[toml]` are left alone.

## Install

```bash
pip install reqexpand
```

The package is not on PyPI yet; install from a clone:

```bash
pip install -e ".[dev]"
```

## Library

```python
from reqexpand import expand_requirements

expand_requirements(
    ["my-app[win]; sys_platform == 'win32'"],
    extras={"win": ["pywin32>=306"]},
    name="my-app",
)
# ['pywin32>=306; sys_platform == "win32"']

expand_requirements(
    ["my-app[win]; sys_platform == 'win32'"],
    extras={"win": ['pywin32>=306; python_version >= "3.10"']},
    name="my-app",
)
# ['pywin32>=306; sys_platform == "win32" and python_version >= "3.10"']
```

A dependency listed under an extra may carry its own `extra == "..."`
clause, as it does in built wheel metadata. Once the extra is expanded into
plain requirements that clause would be evaluated with no extra and the
requirement would never install, so it is settled first: `x; extra == "a"`
under extra `a` becomes `x`, `x; extra == "a" and os_name == "nt"` becomes
`x; os_name == "nt"`, and a dependency tied to a different extra is left out.

`name` is the local project. Only extras on requirements of that project
(after PEP 503 name normalization) are expanded. Nested extras are expanded
too; a cycle raises `CycleError`, and an unknown extra raises `ValueError`.

To take extras from the project's `pyproject.toml` instead of passing the
mapping by hand:

```python
from reqexpand import expand_pyproject

# uses [project].name, [project].dependencies, and
# [project.optional-dependencies]
expand_pyproject("pyproject.toml")

# explicit requirements, extras from the file
expand_pyproject("pyproject.toml", ["my-app[win]; sys_platform == 'win32'"])
```

`expand_pyproject` also accepts a directory that contains `pyproject.toml`.
Pass `extras=` to append more dependencies to the extras declared in the
file, and `name=` to override `[project].name`. The original
`expand_requirements` API is unchanged.

## Command line

```bash
reqexpand --name my-app --extra 'win=pywin32>=306' \
    'my-app[win]; sys_platform == "win32"'
# pywin32>=306; sys_platform == "win32"
```

Pass `--extra NAME=REQUIREMENT` once per dependency. Repeating the extra
name appends another dependency.

Read extras and, unless you pass requirements yourself, `[project].dependencies`
from a `pyproject.toml`:

```bash
reqexpand --pyproject pyproject.toml
reqexpand --pyproject . 'my-app[win]; sys_platform == "win32"'
```

`--name` defaults to `[project].name` when `--pyproject` is used. `--extra`
still works and is appended to the extras from the file.

PEP 735 dependency groups in the same file can be expanded instead of
`[project].dependencies`. Local extras inside a group are still expanded,
and `{include-group = "..."}` entries are followed. A cycle raises
`CycleError`.

```python
expand_pyproject("pyproject.toml", group="dev")
```

```bash
reqexpand --pyproject pyproject.toml --group dev
```

`--group` cannot be combined with requirement arguments. A file that has
only `[dependency-groups]` and no `[project]` table works too; there are no
local extras to expand, so `--name` is not needed.

## Examples

The first two start from this `pyproject.toml`:

```toml
[project]
name = "my-app"
version = "1.0"
dependencies = ["click>=8", "my-app[win]; sys_platform == 'win32'"]

[project.optional-dependencies]
win = ["pywin32>=306", "colorama; python_version < '3.12'"]
docs = ["sphinx>=7"]
all = ["my-app[win,docs]"]

[dependency-groups]
test = ["pytest>=7", "my-app[docs]", "coverage[toml]"]
lint = ["ruff"]
ci = [{ include-group = "test" }, { include-group = "lint" }]
```

**Extras.** The project's own dependencies, with the `win` extra replaced and
its marker kept and combined with the extra's own:

```console
$ reqexpand --pyproject .
click>=8
pywin32>=306; sys_platform == "win32"
colorama; sys_platform == "win32" and python_version < "3.12"
```

Pass a requirement to expand just that one:

```console
$ reqexpand --pyproject . 'my-app[all]'
pywin32>=306
colorama; python_version < '3.12'
sphinx>=7
```

Requirements that need no change are printed exactly as written (note the
single quotes above, copied from `pyproject.toml`). Only a requirement whose
marker had to be combined is rewritten, and then only its marker.

**Dependency groups.** Flatten a group for a CI job. `include-group` is
followed, the project's `docs` extra is expanded, and `coverage[toml]`
is a third-party extra, so it is left as it is:

```console
$ reqexpand --pyproject . --group ci > requirements-ci.txt
$ cat requirements-ci.txt
pytest>=7
sphinx>=7
coverage[toml]
ruff
$ pip install -r requirements-ci.txt
```

**Hatch.** Move `dependencies` and the extras into the hook table described
in the next section (and list both as `dynamic`), and the built wheel carries
no `my-app[...]` requirement:

```text
Requires-Dist: click>=8
Requires-Dist: colorama; sys_platform == 'win32' and python_version < '3.12'
Requires-Dist: pywin32>=306; sys_platform == 'win32'
Provides-Extra: all
Requires-Dist: colorama; (python_version < '3.12') and extra == 'all'
Requires-Dist: pywin32>=306; extra == 'all'
Requires-Dist: sphinx>=7; extra == 'all'
```

## Hatch

A Hatch metadata hook can write the expanded lists into the built
metadata, so a wheel never contains a self-referencing requirement such as
`my-app[win]`. Make the fields dynamic and describe them under the hook
table:

```toml
[build-system]
requires = ["hatchling", "reqexpand"]
build-backend = "hatchling.build"

[project]
name = "my-app"
dynamic = ["dependencies", "optional-dependencies"]

[dependency-groups]
runtime = ["attrs>=23", "colorama; sys_platform == 'win32'"]

[tool.hatch.metadata.hooks.reqexpand]
dependencies = [{ include-group = "runtime" }]

[tool.hatch.metadata.hooks.reqexpand.optional-dependencies]
win = ["pywin32>=306; sys_platform == 'win32'"]
all = ["my-app[win]", { include-group = "runtime" }]
```

Hatch requires a field to be either static or dynamic, so the lists live in
the hook table, not in `[project]`. Entries may name the project's own
extras and may use `{include-group = "..."}` to pull in a PEP 735 group
from `[dependency-groups]`. Parent markers are combined with each
replaced requirement's marker as described above. If `dependencies` is
left out of the hook table, it is not touched. An `extra == "..."` clause on
an extra's own requirement is dropped, since Hatch adds that clause itself,
and a requirement tied to a different extra is left out of this one. Unknown extras, unknown
groups, and cycles fail the build with the same errors as the library.

## Tests

```bash
pip install -e ".[dev]"
pytest
```

## License

MIT
