reqexpand 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- reqexpand-0.1.0/.github/workflows/release.yml +82 -0
- reqexpand-0.1.0/.github/workflows/tests.yml +41 -0
- reqexpand-0.1.0/.gitignore +9 -0
- reqexpand-0.1.0/CHANGELOG.md +23 -0
- reqexpand-0.1.0/LICENSE +21 -0
- reqexpand-0.1.0/PKG-INFO +265 -0
- reqexpand-0.1.0/README.md +231 -0
- reqexpand-0.1.0/pyproject.toml +65 -0
- reqexpand-0.1.0/src/reqexpand/__init__.py +15 -0
- reqexpand-0.1.0/src/reqexpand/__main__.py +3 -0
- reqexpand-0.1.0/src/reqexpand/cli.py +110 -0
- reqexpand-0.1.0/src/reqexpand/expand.py +171 -0
- reqexpand-0.1.0/src/reqexpand/extra_marker.py +112 -0
- reqexpand-0.1.0/src/reqexpand/hatch_plugin.py +72 -0
- reqexpand-0.1.0/src/reqexpand/py.typed +0 -0
- reqexpand-0.1.0/src/reqexpand/pyproject.py +184 -0
- reqexpand-0.1.0/tests/test_cli.py +171 -0
- reqexpand-0.1.0/tests/test_expand.py +218 -0
- reqexpand-0.1.0/tests/test_extra_marker.py +73 -0
- reqexpand-0.1.0/tests/test_hatch_plugin.py +190 -0
- reqexpand-0.1.0/tests/test_pyproject.py +237 -0
- reqexpand-0.1.0/tests/test_real_world.py +86 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
name: release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
|
|
17
|
+
- uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.12"
|
|
20
|
+
|
|
21
|
+
- name: Install build
|
|
22
|
+
run: python -m pip install build twine
|
|
23
|
+
|
|
24
|
+
- name: Build
|
|
25
|
+
run: python -m build
|
|
26
|
+
|
|
27
|
+
- name: Check the distribution
|
|
28
|
+
run: twine check dist/*
|
|
29
|
+
|
|
30
|
+
- name: Verify the tag matches the package version
|
|
31
|
+
run: |
|
|
32
|
+
python - <<'PY'
|
|
33
|
+
import os
|
|
34
|
+
import sys
|
|
35
|
+
import tarfile
|
|
36
|
+
from pathlib import Path
|
|
37
|
+
|
|
38
|
+
tag = os.environ["GITHUB_REF_NAME"]
|
|
39
|
+
if not tag.startswith("v"):
|
|
40
|
+
sys.exit(f"tag {tag!r} does not start with 'v'")
|
|
41
|
+
expected = tag[1:]
|
|
42
|
+
|
|
43
|
+
sdists = list(Path("dist").glob("*.tar.gz"))
|
|
44
|
+
if len(sdists) != 1:
|
|
45
|
+
sys.exit(f"expected one sdist, found {sdists}")
|
|
46
|
+
|
|
47
|
+
with tarfile.open(sdists[0]) as tf:
|
|
48
|
+
member = next(m for m in tf.getmembers() if m.name.endswith("PKG-INFO"))
|
|
49
|
+
text = tf.extractfile(member).read().decode()
|
|
50
|
+
|
|
51
|
+
version = None
|
|
52
|
+
for line in text.splitlines():
|
|
53
|
+
if line.lower().startswith("version:"):
|
|
54
|
+
version = line.split(":", 1)[1].strip()
|
|
55
|
+
break
|
|
56
|
+
if version is None:
|
|
57
|
+
sys.exit("could not read Version from PKG-INFO")
|
|
58
|
+
if version != expected:
|
|
59
|
+
sys.exit(f"tag {tag} does not match package version {version}")
|
|
60
|
+
print(f"tag {tag} matches package version {version}")
|
|
61
|
+
PY
|
|
62
|
+
|
|
63
|
+
- uses: actions/upload-artifact@v4
|
|
64
|
+
with:
|
|
65
|
+
name: dist
|
|
66
|
+
path: dist/
|
|
67
|
+
|
|
68
|
+
publish:
|
|
69
|
+
needs: build
|
|
70
|
+
runs-on: ubuntu-latest
|
|
71
|
+
environment:
|
|
72
|
+
name: pypi
|
|
73
|
+
url: https://pypi.org/p/reqexpand
|
|
74
|
+
permissions:
|
|
75
|
+
id-token: write
|
|
76
|
+
steps:
|
|
77
|
+
- uses: actions/download-artifact@v4
|
|
78
|
+
with:
|
|
79
|
+
name: dist
|
|
80
|
+
path: dist/
|
|
81
|
+
|
|
82
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
name: tests
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches: [main]
|
|
5
|
+
pull_request:
|
|
6
|
+
jobs:
|
|
7
|
+
lint:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
steps:
|
|
10
|
+
- uses: actions/checkout@v4
|
|
11
|
+
- uses: actions/setup-python@v5
|
|
12
|
+
with:
|
|
13
|
+
python-version: "3.12"
|
|
14
|
+
- run: pip install -e ".[dev]"
|
|
15
|
+
- run: ruff format --check src tests
|
|
16
|
+
- run: ruff check src tests
|
|
17
|
+
- run: mypy
|
|
18
|
+
|
|
19
|
+
test:
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
strategy:
|
|
22
|
+
matrix:
|
|
23
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v4
|
|
26
|
+
- uses: actions/setup-python@v5
|
|
27
|
+
with:
|
|
28
|
+
python-version: ${{ matrix.python-version }}
|
|
29
|
+
- run: pip install -e ".[dev]"
|
|
30
|
+
- run: pytest
|
|
31
|
+
|
|
32
|
+
package:
|
|
33
|
+
runs-on: ubuntu-latest
|
|
34
|
+
steps:
|
|
35
|
+
- uses: actions/checkout@v4
|
|
36
|
+
- uses: actions/setup-python@v5
|
|
37
|
+
with:
|
|
38
|
+
python-version: "3.12"
|
|
39
|
+
- run: pip install build twine
|
|
40
|
+
- run: python -m build
|
|
41
|
+
- run: twine check dist/*
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- `expand_requirements` replaces extras of a named project with their
|
|
8
|
+
dependencies and combines the parent marker with each dependency's own
|
|
9
|
+
marker using `and`. Nested extras are followed, cycles raise `CycleError`,
|
|
10
|
+
unknown extras raise `ValueError`, and third-party extras are left alone.
|
|
11
|
+
`extra == "..."` clauses on an extra's own dependencies are settled
|
|
12
|
+
for the extra being expanded. Requirements that need no change keep the
|
|
13
|
+
text they were written with. Duplicate results are listed once, and
|
|
14
|
+
invalid requirement strings are reported with the string and the extra
|
|
15
|
+
they came from.
|
|
16
|
+
- `expand_pyproject` reads `[project]`, `[project.optional-dependencies]` and
|
|
17
|
+
PEP 735 `[dependency-groups]` (including `include-group`) from a
|
|
18
|
+
`pyproject.toml`. A file with no `[project]` table works for groups.
|
|
19
|
+
- `reqexpand` command line (also `python -m reqexpand`) with `--pyproject`,
|
|
20
|
+
`--group`, `--name`, `--extra` and `--version`.
|
|
21
|
+
- Hatch metadata hook (`[tool.hatch.metadata.hooks.reqexpand]`) that writes
|
|
22
|
+
flattened `dependencies` and `optional-dependencies` into built metadata,
|
|
23
|
+
without repeating the `extra == "..."` clause Hatch adds itself.
|
reqexpand-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 driftwright
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
reqexpand-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: reqexpand
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Expand package extras without dropping PEP 508 environment markers.
|
|
5
|
+
Project-URL: Homepage, https://github.com/driftwright/reqexpand
|
|
6
|
+
Project-URL: Issues, https://github.com/driftwright/reqexpand/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/driftwright/reqexpand/blob/main/CHANGELOG.md
|
|
8
|
+
Author: driftwright
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: extras,markers,packaging,pep508,requirements
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Framework :: Hatch
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Requires-Dist: packaging>=23
|
|
25
|
+
Requires-Dist: tomli>=2; python_version < '3.11'
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: build>=1; extra == 'dev'
|
|
28
|
+
Requires-Dist: hatchling>=1.18; extra == 'dev'
|
|
29
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest>=7; extra == 'dev'
|
|
31
|
+
Requires-Dist: ruff==0.17.6; extra == 'dev'
|
|
32
|
+
Requires-Dist: tomli>=2; extra == 'dev'
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# reqexpand
|
|
36
|
+
|
|
37
|
+
Expand a local project's extras into concrete requirements without dropping
|
|
38
|
+
PEP 508 environment markers.
|
|
39
|
+
|
|
40
|
+
A requirement such as `my-app[win]; sys_platform == "win32"` often loses that
|
|
41
|
+
marker when a tool substitutes the `win` extra's dependencies. reqexpand
|
|
42
|
+
replaces extras of the named project with those dependencies and combines
|
|
43
|
+
the parent marker with each dependency's own marker using `and`. Third-party
|
|
44
|
+
extras such as `coverage[toml]` are left alone.
|
|
45
|
+
|
|
46
|
+
## Install
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install reqexpand
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The package is not on PyPI yet; install from a clone:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install -e ".[dev]"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Library
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from reqexpand import expand_requirements
|
|
62
|
+
|
|
63
|
+
expand_requirements(
|
|
64
|
+
["my-app[win]; sys_platform == 'win32'"],
|
|
65
|
+
extras={"win": ["pywin32>=306"]},
|
|
66
|
+
name="my-app",
|
|
67
|
+
)
|
|
68
|
+
# ['pywin32>=306; sys_platform == "win32"']
|
|
69
|
+
|
|
70
|
+
expand_requirements(
|
|
71
|
+
["my-app[win]; sys_platform == 'win32'"],
|
|
72
|
+
extras={"win": ['pywin32>=306; python_version >= "3.10"']},
|
|
73
|
+
name="my-app",
|
|
74
|
+
)
|
|
75
|
+
# ['pywin32>=306; sys_platform == "win32" and python_version >= "3.10"']
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
A dependency listed under an extra may carry its own `extra == "..."`
|
|
79
|
+
clause, as it does in built wheel metadata. Once the extra is expanded into
|
|
80
|
+
plain requirements that clause would be evaluated with no extra and the
|
|
81
|
+
requirement would never install, so it is settled first: `x; extra == "a"`
|
|
82
|
+
under extra `a` becomes `x`, `x; extra == "a" and os_name == "nt"` becomes
|
|
83
|
+
`x; os_name == "nt"`, and a dependency tied to a different extra is left out.
|
|
84
|
+
|
|
85
|
+
`name` is the local project. Only extras on requirements of that project
|
|
86
|
+
(after PEP 503 name normalization) are expanded. Nested extras are expanded
|
|
87
|
+
too; a cycle raises `CycleError`, and an unknown extra raises `ValueError`.
|
|
88
|
+
|
|
89
|
+
To take extras from the project's `pyproject.toml` instead of passing the
|
|
90
|
+
mapping by hand:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from reqexpand import expand_pyproject
|
|
94
|
+
|
|
95
|
+
# uses [project].name, [project].dependencies, and
|
|
96
|
+
# [project.optional-dependencies]
|
|
97
|
+
expand_pyproject("pyproject.toml")
|
|
98
|
+
|
|
99
|
+
# explicit requirements, extras from the file
|
|
100
|
+
expand_pyproject("pyproject.toml", ["my-app[win]; sys_platform == 'win32'"])
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`expand_pyproject` also accepts a directory that contains `pyproject.toml`.
|
|
104
|
+
Pass `extras=` to append more dependencies to the extras declared in the
|
|
105
|
+
file, and `name=` to override `[project].name`. The original
|
|
106
|
+
`expand_requirements` API is unchanged.
|
|
107
|
+
|
|
108
|
+
## Command line
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
reqexpand --name my-app --extra 'win=pywin32>=306' \
|
|
112
|
+
'my-app[win]; sys_platform == "win32"'
|
|
113
|
+
# pywin32>=306; sys_platform == "win32"
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Pass `--extra NAME=REQUIREMENT` once per dependency. Repeating the extra
|
|
117
|
+
name appends another dependency.
|
|
118
|
+
|
|
119
|
+
Read extras and, unless you pass requirements yourself, `[project].dependencies`
|
|
120
|
+
from a `pyproject.toml`:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
reqexpand --pyproject pyproject.toml
|
|
124
|
+
reqexpand --pyproject . 'my-app[win]; sys_platform == "win32"'
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`--name` defaults to `[project].name` when `--pyproject` is used. `--extra`
|
|
128
|
+
still works and is appended to the extras from the file.
|
|
129
|
+
|
|
130
|
+
PEP 735 dependency groups in the same file can be expanded instead of
|
|
131
|
+
`[project].dependencies`. Local extras inside a group are still expanded,
|
|
132
|
+
and `{include-group = "..."}` entries are followed. A cycle raises
|
|
133
|
+
`CycleError`.
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
expand_pyproject("pyproject.toml", group="dev")
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
reqexpand --pyproject pyproject.toml --group dev
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
`--group` cannot be combined with requirement arguments. A file that has
|
|
144
|
+
only `[dependency-groups]` and no `[project]` table works too; there are no
|
|
145
|
+
local extras to expand, so `--name` is not needed.
|
|
146
|
+
|
|
147
|
+
## Examples
|
|
148
|
+
|
|
149
|
+
The first two start from this `pyproject.toml`:
|
|
150
|
+
|
|
151
|
+
```toml
|
|
152
|
+
[project]
|
|
153
|
+
name = "my-app"
|
|
154
|
+
version = "1.0"
|
|
155
|
+
dependencies = ["click>=8", "my-app[win]; sys_platform == 'win32'"]
|
|
156
|
+
|
|
157
|
+
[project.optional-dependencies]
|
|
158
|
+
win = ["pywin32>=306", "colorama; python_version < '3.12'"]
|
|
159
|
+
docs = ["sphinx>=7"]
|
|
160
|
+
all = ["my-app[win,docs]"]
|
|
161
|
+
|
|
162
|
+
[dependency-groups]
|
|
163
|
+
test = ["pytest>=7", "my-app[docs]", "coverage[toml]"]
|
|
164
|
+
lint = ["ruff"]
|
|
165
|
+
ci = [{ include-group = "test" }, { include-group = "lint" }]
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**Extras.** The project's own dependencies, with the `win` extra replaced and
|
|
169
|
+
its marker kept and combined with the extra's own:
|
|
170
|
+
|
|
171
|
+
```console
|
|
172
|
+
$ reqexpand --pyproject .
|
|
173
|
+
click>=8
|
|
174
|
+
pywin32>=306; sys_platform == "win32"
|
|
175
|
+
colorama; sys_platform == "win32" and python_version < "3.12"
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Pass a requirement to expand just that one:
|
|
179
|
+
|
|
180
|
+
```console
|
|
181
|
+
$ reqexpand --pyproject . 'my-app[all]'
|
|
182
|
+
pywin32>=306
|
|
183
|
+
colorama; python_version < '3.12'
|
|
184
|
+
sphinx>=7
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Requirements that need no change are printed exactly as written (note the
|
|
188
|
+
single quotes above, copied from `pyproject.toml`). Only a requirement whose
|
|
189
|
+
marker had to be combined is rewritten, and then only its marker.
|
|
190
|
+
|
|
191
|
+
**Dependency groups.** Flatten a group for a CI job. `include-group` is
|
|
192
|
+
followed, the project's `docs` extra is expanded, and `coverage[toml]`
|
|
193
|
+
is a third-party extra, so it is left as it is:
|
|
194
|
+
|
|
195
|
+
```console
|
|
196
|
+
$ reqexpand --pyproject . --group ci > requirements-ci.txt
|
|
197
|
+
$ cat requirements-ci.txt
|
|
198
|
+
pytest>=7
|
|
199
|
+
sphinx>=7
|
|
200
|
+
coverage[toml]
|
|
201
|
+
ruff
|
|
202
|
+
$ pip install -r requirements-ci.txt
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
**Hatch.** Move `dependencies` and the extras into the hook table described
|
|
206
|
+
in the next section (and list both as `dynamic`), and the built wheel carries
|
|
207
|
+
no `my-app[...]` requirement:
|
|
208
|
+
|
|
209
|
+
```text
|
|
210
|
+
Requires-Dist: click>=8
|
|
211
|
+
Requires-Dist: colorama; sys_platform == 'win32' and python_version < '3.12'
|
|
212
|
+
Requires-Dist: pywin32>=306; sys_platform == 'win32'
|
|
213
|
+
Provides-Extra: all
|
|
214
|
+
Requires-Dist: colorama; (python_version < '3.12') and extra == 'all'
|
|
215
|
+
Requires-Dist: pywin32>=306; extra == 'all'
|
|
216
|
+
Requires-Dist: sphinx>=7; extra == 'all'
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Hatch
|
|
220
|
+
|
|
221
|
+
A Hatch metadata hook can write the expanded lists into the built
|
|
222
|
+
metadata, so a wheel never contains a self-referencing requirement such as
|
|
223
|
+
`my-app[win]`. Make the fields dynamic and describe them under the hook
|
|
224
|
+
table:
|
|
225
|
+
|
|
226
|
+
```toml
|
|
227
|
+
[build-system]
|
|
228
|
+
requires = ["hatchling", "reqexpand"]
|
|
229
|
+
build-backend = "hatchling.build"
|
|
230
|
+
|
|
231
|
+
[project]
|
|
232
|
+
name = "my-app"
|
|
233
|
+
dynamic = ["dependencies", "optional-dependencies"]
|
|
234
|
+
|
|
235
|
+
[dependency-groups]
|
|
236
|
+
runtime = ["attrs>=23", "colorama; sys_platform == 'win32'"]
|
|
237
|
+
|
|
238
|
+
[tool.hatch.metadata.hooks.reqexpand]
|
|
239
|
+
dependencies = [{ include-group = "runtime" }]
|
|
240
|
+
|
|
241
|
+
[tool.hatch.metadata.hooks.reqexpand.optional-dependencies]
|
|
242
|
+
win = ["pywin32>=306; sys_platform == 'win32'"]
|
|
243
|
+
all = ["my-app[win]", { include-group = "runtime" }]
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Hatch requires a field to be either static or dynamic, so the lists live in
|
|
247
|
+
the hook table, not in `[project]`. Entries may name the project's own
|
|
248
|
+
extras and may use `{include-group = "..."}` to pull in a PEP 735 group
|
|
249
|
+
from `[dependency-groups]`. Parent markers are combined with each
|
|
250
|
+
replaced requirement's marker as described above. If `dependencies` is
|
|
251
|
+
left out of the hook table, it is not touched. An `extra == "..."` clause on
|
|
252
|
+
an extra's own requirement is dropped, since Hatch adds that clause itself,
|
|
253
|
+
and a requirement tied to a different extra is left out of this one. Unknown extras, unknown
|
|
254
|
+
groups, and cycles fail the build with the same errors as the library.
|
|
255
|
+
|
|
256
|
+
## Tests
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
pip install -e ".[dev]"
|
|
260
|
+
pytest
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## License
|
|
264
|
+
|
|
265
|
+
MIT
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# reqexpand
|
|
2
|
+
|
|
3
|
+
Expand a local project's extras into concrete requirements without dropping
|
|
4
|
+
PEP 508 environment markers.
|
|
5
|
+
|
|
6
|
+
A requirement such as `my-app[win]; sys_platform == "win32"` often loses that
|
|
7
|
+
marker when a tool substitutes the `win` extra's dependencies. reqexpand
|
|
8
|
+
replaces extras of the named project with those dependencies and combines
|
|
9
|
+
the parent marker with each dependency's own marker using `and`. Third-party
|
|
10
|
+
extras such as `coverage[toml]` are left alone.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install reqexpand
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
The package is not on PyPI yet; install from a clone:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install -e ".[dev]"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Library
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from reqexpand import expand_requirements
|
|
28
|
+
|
|
29
|
+
expand_requirements(
|
|
30
|
+
["my-app[win]; sys_platform == 'win32'"],
|
|
31
|
+
extras={"win": ["pywin32>=306"]},
|
|
32
|
+
name="my-app",
|
|
33
|
+
)
|
|
34
|
+
# ['pywin32>=306; sys_platform == "win32"']
|
|
35
|
+
|
|
36
|
+
expand_requirements(
|
|
37
|
+
["my-app[win]; sys_platform == 'win32'"],
|
|
38
|
+
extras={"win": ['pywin32>=306; python_version >= "3.10"']},
|
|
39
|
+
name="my-app",
|
|
40
|
+
)
|
|
41
|
+
# ['pywin32>=306; sys_platform == "win32" and python_version >= "3.10"']
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
A dependency listed under an extra may carry its own `extra == "..."`
|
|
45
|
+
clause, as it does in built wheel metadata. Once the extra is expanded into
|
|
46
|
+
plain requirements that clause would be evaluated with no extra and the
|
|
47
|
+
requirement would never install, so it is settled first: `x; extra == "a"`
|
|
48
|
+
under extra `a` becomes `x`, `x; extra == "a" and os_name == "nt"` becomes
|
|
49
|
+
`x; os_name == "nt"`, and a dependency tied to a different extra is left out.
|
|
50
|
+
|
|
51
|
+
`name` is the local project. Only extras on requirements of that project
|
|
52
|
+
(after PEP 503 name normalization) are expanded. Nested extras are expanded
|
|
53
|
+
too; a cycle raises `CycleError`, and an unknown extra raises `ValueError`.
|
|
54
|
+
|
|
55
|
+
To take extras from the project's `pyproject.toml` instead of passing the
|
|
56
|
+
mapping by hand:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from reqexpand import expand_pyproject
|
|
60
|
+
|
|
61
|
+
# uses [project].name, [project].dependencies, and
|
|
62
|
+
# [project.optional-dependencies]
|
|
63
|
+
expand_pyproject("pyproject.toml")
|
|
64
|
+
|
|
65
|
+
# explicit requirements, extras from the file
|
|
66
|
+
expand_pyproject("pyproject.toml", ["my-app[win]; sys_platform == 'win32'"])
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`expand_pyproject` also accepts a directory that contains `pyproject.toml`.
|
|
70
|
+
Pass `extras=` to append more dependencies to the extras declared in the
|
|
71
|
+
file, and `name=` to override `[project].name`. The original
|
|
72
|
+
`expand_requirements` API is unchanged.
|
|
73
|
+
|
|
74
|
+
## Command line
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
reqexpand --name my-app --extra 'win=pywin32>=306' \
|
|
78
|
+
'my-app[win]; sys_platform == "win32"'
|
|
79
|
+
# pywin32>=306; sys_platform == "win32"
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Pass `--extra NAME=REQUIREMENT` once per dependency. Repeating the extra
|
|
83
|
+
name appends another dependency.
|
|
84
|
+
|
|
85
|
+
Read extras and, unless you pass requirements yourself, `[project].dependencies`
|
|
86
|
+
from a `pyproject.toml`:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
reqexpand --pyproject pyproject.toml
|
|
90
|
+
reqexpand --pyproject . 'my-app[win]; sys_platform == "win32"'
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`--name` defaults to `[project].name` when `--pyproject` is used. `--extra`
|
|
94
|
+
still works and is appended to the extras from the file.
|
|
95
|
+
|
|
96
|
+
PEP 735 dependency groups in the same file can be expanded instead of
|
|
97
|
+
`[project].dependencies`. Local extras inside a group are still expanded,
|
|
98
|
+
and `{include-group = "..."}` entries are followed. A cycle raises
|
|
99
|
+
`CycleError`.
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
expand_pyproject("pyproject.toml", group="dev")
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
reqexpand --pyproject pyproject.toml --group dev
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`--group` cannot be combined with requirement arguments. A file that has
|
|
110
|
+
only `[dependency-groups]` and no `[project]` table works too; there are no
|
|
111
|
+
local extras to expand, so `--name` is not needed.
|
|
112
|
+
|
|
113
|
+
## Examples
|
|
114
|
+
|
|
115
|
+
The first two start from this `pyproject.toml`:
|
|
116
|
+
|
|
117
|
+
```toml
|
|
118
|
+
[project]
|
|
119
|
+
name = "my-app"
|
|
120
|
+
version = "1.0"
|
|
121
|
+
dependencies = ["click>=8", "my-app[win]; sys_platform == 'win32'"]
|
|
122
|
+
|
|
123
|
+
[project.optional-dependencies]
|
|
124
|
+
win = ["pywin32>=306", "colorama; python_version < '3.12'"]
|
|
125
|
+
docs = ["sphinx>=7"]
|
|
126
|
+
all = ["my-app[win,docs]"]
|
|
127
|
+
|
|
128
|
+
[dependency-groups]
|
|
129
|
+
test = ["pytest>=7", "my-app[docs]", "coverage[toml]"]
|
|
130
|
+
lint = ["ruff"]
|
|
131
|
+
ci = [{ include-group = "test" }, { include-group = "lint" }]
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Extras.** The project's own dependencies, with the `win` extra replaced and
|
|
135
|
+
its marker kept and combined with the extra's own:
|
|
136
|
+
|
|
137
|
+
```console
|
|
138
|
+
$ reqexpand --pyproject .
|
|
139
|
+
click>=8
|
|
140
|
+
pywin32>=306; sys_platform == "win32"
|
|
141
|
+
colorama; sys_platform == "win32" and python_version < "3.12"
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Pass a requirement to expand just that one:
|
|
145
|
+
|
|
146
|
+
```console
|
|
147
|
+
$ reqexpand --pyproject . 'my-app[all]'
|
|
148
|
+
pywin32>=306
|
|
149
|
+
colorama; python_version < '3.12'
|
|
150
|
+
sphinx>=7
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Requirements that need no change are printed exactly as written (note the
|
|
154
|
+
single quotes above, copied from `pyproject.toml`). Only a requirement whose
|
|
155
|
+
marker had to be combined is rewritten, and then only its marker.
|
|
156
|
+
|
|
157
|
+
**Dependency groups.** Flatten a group for a CI job. `include-group` is
|
|
158
|
+
followed, the project's `docs` extra is expanded, and `coverage[toml]`
|
|
159
|
+
is a third-party extra, so it is left as it is:
|
|
160
|
+
|
|
161
|
+
```console
|
|
162
|
+
$ reqexpand --pyproject . --group ci > requirements-ci.txt
|
|
163
|
+
$ cat requirements-ci.txt
|
|
164
|
+
pytest>=7
|
|
165
|
+
sphinx>=7
|
|
166
|
+
coverage[toml]
|
|
167
|
+
ruff
|
|
168
|
+
$ pip install -r requirements-ci.txt
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**Hatch.** Move `dependencies` and the extras into the hook table described
|
|
172
|
+
in the next section (and list both as `dynamic`), and the built wheel carries
|
|
173
|
+
no `my-app[...]` requirement:
|
|
174
|
+
|
|
175
|
+
```text
|
|
176
|
+
Requires-Dist: click>=8
|
|
177
|
+
Requires-Dist: colorama; sys_platform == 'win32' and python_version < '3.12'
|
|
178
|
+
Requires-Dist: pywin32>=306; sys_platform == 'win32'
|
|
179
|
+
Provides-Extra: all
|
|
180
|
+
Requires-Dist: colorama; (python_version < '3.12') and extra == 'all'
|
|
181
|
+
Requires-Dist: pywin32>=306; extra == 'all'
|
|
182
|
+
Requires-Dist: sphinx>=7; extra == 'all'
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Hatch
|
|
186
|
+
|
|
187
|
+
A Hatch metadata hook can write the expanded lists into the built
|
|
188
|
+
metadata, so a wheel never contains a self-referencing requirement such as
|
|
189
|
+
`my-app[win]`. Make the fields dynamic and describe them under the hook
|
|
190
|
+
table:
|
|
191
|
+
|
|
192
|
+
```toml
|
|
193
|
+
[build-system]
|
|
194
|
+
requires = ["hatchling", "reqexpand"]
|
|
195
|
+
build-backend = "hatchling.build"
|
|
196
|
+
|
|
197
|
+
[project]
|
|
198
|
+
name = "my-app"
|
|
199
|
+
dynamic = ["dependencies", "optional-dependencies"]
|
|
200
|
+
|
|
201
|
+
[dependency-groups]
|
|
202
|
+
runtime = ["attrs>=23", "colorama; sys_platform == 'win32'"]
|
|
203
|
+
|
|
204
|
+
[tool.hatch.metadata.hooks.reqexpand]
|
|
205
|
+
dependencies = [{ include-group = "runtime" }]
|
|
206
|
+
|
|
207
|
+
[tool.hatch.metadata.hooks.reqexpand.optional-dependencies]
|
|
208
|
+
win = ["pywin32>=306; sys_platform == 'win32'"]
|
|
209
|
+
all = ["my-app[win]", { include-group = "runtime" }]
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Hatch requires a field to be either static or dynamic, so the lists live in
|
|
213
|
+
the hook table, not in `[project]`. Entries may name the project's own
|
|
214
|
+
extras and may use `{include-group = "..."}` to pull in a PEP 735 group
|
|
215
|
+
from `[dependency-groups]`. Parent markers are combined with each
|
|
216
|
+
replaced requirement's marker as described above. If `dependencies` is
|
|
217
|
+
left out of the hook table, it is not touched. An `extra == "..."` clause on
|
|
218
|
+
an extra's own requirement is dropped, since Hatch adds that clause itself,
|
|
219
|
+
and a requirement tied to a different extra is left out of this one. Unknown extras, unknown
|
|
220
|
+
groups, and cycles fail the build with the same errors as the library.
|
|
221
|
+
|
|
222
|
+
## Tests
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
pip install -e ".[dev]"
|
|
226
|
+
pytest
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## License
|
|
230
|
+
|
|
231
|
+
MIT
|