Skip to content

Commit 929003d

Browse files
authored
Merge pull request #3 from python-project-templates/tkp/union
Allow primary to be union of extras
2 parents fb748ad + 9c2de88 commit 929003d

File tree

8 files changed

+249
-120
lines changed

8 files changed

+249
-120
lines changed

hatch_multi/plugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ def update(self, metadata: dict) -> None:
3636
metadata["name"] = config.name
3737
if config.primary:
3838
self._logger.info(f"Setting metadata for primary dependency set '{config.primary}' in hatch-multi")
39-
metadata["dependencies"] = metadata["optional-dependencies"].get(config.primary, [])
39+
if isinstance(config.primary, list):
40+
metadata["dependencies"] = [dep for extra in config.primary for dep in metadata["optional-dependencies"].get(extra, [])]
41+
else:
42+
metadata["dependencies"] = metadata["optional-dependencies"].get(config.primary, [])
4043
else:
4144
self._logger.info("Setting metadata for default dependency set in hatch-multi")
4245
# If no primary is set, use the first extra as default

hatch_multi/structs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Optional
3+
from typing import List, Optional, Union
44

55
from pydantic import AliasChoices, BaseModel, Field
66

@@ -9,8 +9,8 @@
99

1010
class HatchMultiConfig(BaseModel, validate_assignment=True):
1111
name: str = Field(description="Name of the package")
12-
primary: Optional[str] = Field(
12+
primary: Optional[Union[str, List[str]]] = Field(
1313
default=None,
14-
description="Optional dependecy set to use as primary",
14+
description="Optional dependency set/s to use as primary",
1515
alias=AliasChoices("primary", "default"),
1616
)

hatch_multi/tests/test_project_basic/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66
name = "hatch-cpp-test-project-basic"
77
description = "Basic test project for hatch-cpp"
88
version = "0.1.0"
9-
requires-python = ">=3.9"
9+
requires-python = ">=3.11"
1010
dynamic = ["dependencies"]
1111

1212
[project.optional-dependencies]

hatch_multi/tests/test_project_multiple_primary/project/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[build-system]
2+
requires = ["hatchling", "hatch-multi"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "hatch-cpp-test-project-basic"
7+
description = "Basic test project for hatch-cpp"
8+
version = "0.1.0"
9+
requires-python = ">=3.11"
10+
dynamic = ["dependencies"]
11+
12+
[project.optional-dependencies]
13+
main = ["superstore"]
14+
other = ["organizeit2"]
15+
16+
[tool.hatch.build.sources]
17+
src = "/"
18+
19+
[tool.hatch.build.targets.sdist]
20+
packages = ["project"]
21+
22+
[tool.hatch.build.targets.wheel]
23+
packages = ["project"]
24+
25+
[tool.hatch.metadata.hooks.hatch-multi]
26+
default = ["main", "other"]

hatch_multi/tests/test_projects.py

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from os import listdir
2+
from pathlib import Path
3+
from shutil import rmtree
4+
from subprocess import check_call
5+
from sys import executable
6+
from zipfile import ZipFile
7+
8+
9+
def test_basic():
10+
project = "test_project_basic"
11+
try:
12+
rmtree(f"hatch_multi/tests/{project}/dist")
13+
except FileNotFoundError:
14+
pass
15+
16+
check_call(
17+
[
18+
executable,
19+
"-m",
20+
"build",
21+
"-n",
22+
"-w",
23+
],
24+
cwd=f"hatch_multi/tests/{project}",
25+
env={"SKIP_HATCH_MULTI": "1"},
26+
)
27+
28+
assert Path(f"hatch_multi/tests/{project}/dist").exists()
29+
assert listdir(f"hatch_multi/tests/{project}/dist") == ["hatch_cpp_test_project_basic-0.1.0-py3-none-any.whl"]
30+
with ZipFile(f"hatch_multi/tests/{project}/dist/hatch_cpp_test_project_basic-0.1.0-py3-none-any.whl", "r") as zip_ref:
31+
zip_ref.extractall(f"hatch_multi/tests/{project}/dist/extracted")
32+
assert (
33+
Path(f"hatch_multi/tests/{project}/dist/extracted").joinpath("hatch_cpp_test_project_basic-0.1.0.dist-info/METADATA").read_text()
34+
== """Metadata-Version: 2.4
35+
Name: hatch-cpp-test-project-basic
36+
Version: 0.1.0
37+
Dynamic: Requires-Dist
38+
Summary: Basic test project for hatch-cpp
39+
Requires-Python: >=3.11
40+
Provides-Extra: main
41+
Requires-Dist: superstore; extra == 'main'
42+
Provides-Extra: other
43+
Requires-Dist: organizeit2; extra == 'other'
44+
"""
45+
)
46+
rmtree(f"hatch_multi/tests/{project}/dist")
47+
48+
check_call(
49+
[
50+
executable,
51+
"-m",
52+
"build",
53+
"-n",
54+
"-w",
55+
],
56+
cwd=f"hatch_multi/tests/{project}",
57+
)
58+
59+
assert Path(f"hatch_multi/tests/{project}/dist").exists()
60+
assert listdir(f"hatch_multi/tests/{project}/dist") == ["hatch_cpp_test_project_basic-0.1.0-py3-none-any.whl"]
61+
with ZipFile(f"hatch_multi/tests/{project}/dist/hatch_cpp_test_project_basic-0.1.0-py3-none-any.whl", "r") as zip_ref:
62+
zip_ref.extractall(f"hatch_multi/tests/{project}/dist/extracted")
63+
assert (
64+
Path(f"hatch_multi/tests/{project}/dist/extracted").joinpath("hatch_cpp_test_project_basic-0.1.0.dist-info/METADATA").read_text()
65+
== """Metadata-Version: 2.4
66+
Name: hatch-cpp-test-project-basic
67+
Version: 0.1.0
68+
Summary: Basic test project for hatch-cpp
69+
Requires-Python: >=3.11
70+
Requires-Dist: superstore
71+
Provides-Extra: main
72+
Requires-Dist: superstore; extra == 'main'
73+
Provides-Extra: other
74+
Requires-Dist: organizeit2; extra == 'other'
75+
"""
76+
)
77+
rmtree(f"hatch_multi/tests/{project}/dist")
78+
79+
check_call(
80+
[
81+
executable,
82+
"-m",
83+
"build",
84+
"-n",
85+
"-w",
86+
],
87+
cwd=f"hatch_multi/tests/{project}",
88+
env={"HATCH_MULTI_BUILD": "other"},
89+
)
90+
91+
assert Path(f"hatch_multi/tests/{project}/dist").exists()
92+
assert listdir(f"hatch_multi/tests/{project}/dist") == ["hatch_cpp_test_project_basic_other-0.1.0-py3-none-any.whl"]
93+
with ZipFile(f"hatch_multi/tests/{project}/dist/hatch_cpp_test_project_basic_other-0.1.0-py3-none-any.whl", "r") as zip_ref:
94+
zip_ref.extractall(f"hatch_multi/tests/{project}/dist/extracted")
95+
assert (
96+
Path(f"hatch_multi/tests/{project}/dist/extracted").joinpath("hatch_cpp_test_project_basic_other-0.1.0.dist-info/METADATA").read_text()
97+
== """Metadata-Version: 2.4
98+
Name: hatch-cpp-test-project-basic-other
99+
Version: 0.1.0
100+
Summary: Basic test project for hatch-cpp
101+
Requires-Python: >=3.11
102+
Requires-Dist: organizeit2
103+
Provides-Extra: main
104+
Requires-Dist: superstore; extra == 'main'
105+
"""
106+
)
107+
rmtree(f"hatch_multi/tests/{project}/dist")
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
from os import listdir
2+
from pathlib import Path
3+
from shutil import rmtree
4+
from subprocess import check_call
5+
from sys import executable
6+
from zipfile import ZipFile
7+
8+
9+
def test_project_basic_multiple_primary():
10+
project = "test_project_multiple_primary"
11+
try:
12+
rmtree(f"hatch_multi/tests/{project}/dist")
13+
except FileNotFoundError:
14+
pass
15+
16+
check_call(
17+
[
18+
executable,
19+
"-m",
20+
"build",
21+
"-n",
22+
"-w",
23+
],
24+
cwd=f"hatch_multi/tests/{project}",
25+
env={"SKIP_HATCH_MULTI": "1"},
26+
)
27+
28+
assert Path(f"hatch_multi/tests/{project}/dist").exists()
29+
assert listdir(f"hatch_multi/tests/{project}/dist") == ["hatch_cpp_test_project_basic-0.1.0-py3-none-any.whl"]
30+
with ZipFile(f"hatch_multi/tests/{project}/dist/hatch_cpp_test_project_basic-0.1.0-py3-none-any.whl", "r") as zip_ref:
31+
zip_ref.extractall(f"hatch_multi/tests/{project}/dist/extracted")
32+
assert (
33+
Path(f"hatch_multi/tests/{project}/dist/extracted").joinpath("hatch_cpp_test_project_basic-0.1.0.dist-info/METADATA").read_text()
34+
== """Metadata-Version: 2.4
35+
Name: hatch-cpp-test-project-basic
36+
Version: 0.1.0
37+
Dynamic: Requires-Dist
38+
Summary: Basic test project for hatch-cpp
39+
Requires-Python: >=3.11
40+
Provides-Extra: main
41+
Requires-Dist: superstore; extra == 'main'
42+
Provides-Extra: other
43+
Requires-Dist: organizeit2; extra == 'other'
44+
"""
45+
)
46+
rmtree(f"hatch_multi/tests/{project}/dist")
47+
48+
check_call(
49+
[
50+
executable,
51+
"-m",
52+
"build",
53+
"-n",
54+
"-w",
55+
],
56+
cwd=f"hatch_multi/tests/{project}",
57+
)
58+
59+
assert Path(f"hatch_multi/tests/{project}/dist").exists()
60+
assert listdir(f"hatch_multi/tests/{project}/dist") == ["hatch_cpp_test_project_basic-0.1.0-py3-none-any.whl"]
61+
with ZipFile(f"hatch_multi/tests/{project}/dist/hatch_cpp_test_project_basic-0.1.0-py3-none-any.whl", "r") as zip_ref:
62+
zip_ref.extractall(f"hatch_multi/tests/{project}/dist/extracted")
63+
assert (
64+
Path(f"hatch_multi/tests/{project}/dist/extracted").joinpath("hatch_cpp_test_project_basic-0.1.0.dist-info/METADATA").read_text()
65+
== """Metadata-Version: 2.4
66+
Name: hatch-cpp-test-project-basic
67+
Version: 0.1.0
68+
Summary: Basic test project for hatch-cpp
69+
Requires-Python: >=3.11
70+
Requires-Dist: organizeit2
71+
Requires-Dist: superstore
72+
Provides-Extra: main
73+
Requires-Dist: superstore; extra == 'main'
74+
Provides-Extra: other
75+
Requires-Dist: organizeit2; extra == 'other'
76+
"""
77+
)
78+
rmtree(f"hatch_multi/tests/{project}/dist")
79+
80+
check_call(
81+
[
82+
executable,
83+
"-m",
84+
"build",
85+
"-n",
86+
"-w",
87+
],
88+
cwd=f"hatch_multi/tests/{project}",
89+
env={"HATCH_MULTI_BUILD": "other"},
90+
)
91+
92+
assert Path(f"hatch_multi/tests/{project}/dist").exists()
93+
assert listdir(f"hatch_multi/tests/{project}/dist") == ["hatch_cpp_test_project_basic_other-0.1.0-py3-none-any.whl"]
94+
with ZipFile(f"hatch_multi/tests/{project}/dist/hatch_cpp_test_project_basic_other-0.1.0-py3-none-any.whl", "r") as zip_ref:
95+
zip_ref.extractall(f"hatch_multi/tests/{project}/dist/extracted")
96+
assert (
97+
Path(f"hatch_multi/tests/{project}/dist/extracted").joinpath("hatch_cpp_test_project_basic_other-0.1.0.dist-info/METADATA").read_text()
98+
== """Metadata-Version: 2.4
99+
Name: hatch-cpp-test-project-basic-other
100+
Version: 0.1.0
101+
Summary: Basic test project for hatch-cpp
102+
Requires-Python: >=3.11
103+
Requires-Dist: organizeit2
104+
Provides-Extra: main
105+
Requires-Dist: superstore; extra == 'main'
106+
"""
107+
)
108+
rmtree(f"hatch_multi/tests/{project}/dist")

0 commit comments

Comments
 (0)