Skip to content

Commit 87a6aaa

Browse files
authored
hatch-dependency-coversion: handle optional deps (#13)
They're in a different part of the metadata.
1 parent 015abcb commit 87a6aaa

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Handle optional dependencies
2+
3+
Also update any optional dependencies if they're present in the plugin config.
4+

hatch-dependency-coversion/src/hatch_dependency_coversion/metadata_hook.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ def _update_dependency_versions(
3939
for depspec in dependencies_metadata
4040
]
4141

42+
def _update_optional_dependency_versions(
43+
self,
44+
optional_dependencies_metadata: dict[str, list[str]],
45+
version: str,
46+
which_dependencies: list[str],
47+
) -> dict[str, list[str]]:
48+
"""Do the actual optional dependency update"""
49+
return {
50+
extra: [
51+
self._maybe_update_dep(depspec, version, which_dependencies)
52+
for depspec in optional_dep_list
53+
]
54+
for extra, optional_dep_list in optional_dependencies_metadata.items()
55+
}
56+
4257
def update(self, metadata: dict[str, Any]) -> None:
4358
"""Update metadata for coversioning."""
4459
# this is from https://github.com/flying-sheep/hatch-docstring-description/blob/main/src/hatch_docstring_description/read_description.py
@@ -54,9 +69,18 @@ def update(self, metadata: dict[str, Any]) -> None:
5469
"tool.hatch.metadata.hooks.dependency-coversion.override-versions-of must be an array of strings"
5570
)
5671
override_of: list[str] = self.config["override-versions-of"]
57-
metadata["dependencies"] = self._update_dependency_versions(
58-
metadata.get("dependencies", []), metadata["version"], override_of
59-
)
72+
if "dependencies" in metadata:
73+
metadata["dependencies"] = self._update_dependency_versions(
74+
metadata.get("dependencies", []), metadata["version"], override_of
75+
)
76+
if "optional-dependencies" in metadata:
77+
metadata[
78+
"optional-dependencies"
79+
] = self._update_optional_dependency_versions(
80+
metadata.get("optional-dependencies", {}),
81+
metadata["version"],
82+
override_of,
83+
)
6084

6185
def get_known_classifiers(self) -> list[str]:
6286
"""Dummy function that is part of the hook interface."""

hatch-dependency-coversion/tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,32 @@ def requests_multiple_project(tmp_path: Path, static_requirements: list[str]) ->
160160
(tmp_path / "coversion_of_multiple_project").mkdir()
161161
(tmp_path / "coversion_of_multiple_project" / "__init__.py").write_text("")
162162
return tmp_path
163+
164+
165+
@pytest.fixture
166+
def requests_coversion_of_optional_dependency_project(
167+
tmp_path: Path, static_requirements: list[str]
168+
) -> Path:
169+
(tmp_path / "pyproject.toml").write_text(
170+
dedent(
171+
f"""
172+
[build-system]
173+
requires = ["hatchling", "hatch-dependency-coversion"]
174+
build-backend = "hatchling.build"
175+
[project]
176+
name = "coversion-of-optional-dependency-project"
177+
version = "0.1.0"
178+
dependencies = [{','.join([f'"{requirement}"' for requirement in static_requirements[1:]])}]
179+
dynamic = ['dependency-coversion']
180+
[project.optional-dependencies]
181+
extra1 = [{f'"{static_requirements[0]}"'}]
182+
[tool.hatch.metadata.hooks.dependency-coversion]
183+
override-versions-of=["dependency1"]
184+
"""
185+
)
186+
)
187+
(tmp_path / "coversion_of_optional_dependency_project").mkdir()
188+
(tmp_path / "coversion_of_optional_dependency_project" / "__init__.py").write_text(
189+
""
190+
)
191+
return tmp_path

hatch-dependency-coversion/tests/test_metadata_hook.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,16 @@ def test_coversion_of_marked_version_applies(
5353
assert sorted(requires) == static_requirements[:2] + [
5454
"dependency3==0.1.0; os_name == 'Windows'"
5555
]
56+
57+
58+
def test_coversion_of_optional_dependency_applies(
59+
requests_coversion_of_optional_dependency_project: Path,
60+
static_requirements: list[str],
61+
) -> None:
62+
wheelpath = build_wheel(requests_coversion_of_optional_dependency_project)
63+
dist_metadata = pkginfo.Wheel(str(wheelpath))
64+
requires = dist_metadata.requires_dist
65+
assert (
66+
sorted(requires)
67+
== ["dependency1==0.1.0; extra == 'extra1'"] + static_requirements[1:]
68+
)

0 commit comments

Comments
 (0)