Skip to main content

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

pip install reqexpand

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

pip install -e ".[dev]"

Library

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:

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

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:

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.

expand_pyproject("pyproject.toml", group="dev")
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:

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

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

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

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

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:

[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

pip install -e ".[dev]"
pytest

License

MIT

Metadata

Release files for reqexpand 0.1.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 reqexpand 0.1.0
File Size Uploaded Provenance Actions
reqexpand-0.1.0.tar.gz 18.1 kB Without provenance Details for reqexpand-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for reqexpand 0.1.0
File Interpreter ABI Platform Actions
reqexpand-0.1.0-py3-none-any.whl Python 3 none any Details for reqexpand-0.1.0-py3-none-any.whl

Total release size: 32.2 kB

Release files / reqexpand-0.1.0.tar.gz

Download URL reqexpand-0.1.0.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7a1eb40c6cb983da40bc932fac826f104bbc5312c84f4e22c741d96d377bf28e
BLAKE2b-256 checksum
How to use checksums
2e266b05a3b019859ee902ef24fcdfc79f632a3056ffcff6e7050e0e6f7398de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / reqexpand-0.1.0-py3-none-any.whl

Download URL reqexpand-0.1.0-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02ab9d995a0cf7e6e98dc762cab599e1007f7970b991926210ca5b0e823b2ee7
BLAKE2b-256 checksum
How to use checksums
521c16bd5aaae538b30d8b48a5b885ea1f77ad3cdb538f94a6a18cb92cc6e0d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Security for reqexpand 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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