Skip to content

Commit ad99369

Browse files
authored
fix(poly libs): handle third-party dependencies with markers, such as multi-python versions (#427)
* fix(poly libs): handle third-party dependencies with markers, such as multi python versions * bump hatch bricks hook to 1.5.5 * bump pdm bricks hook to 1.3.7 * bump pdm workspace hook to 1.3.7 * bump Polylith plugin to 1.48.4 * bump CLI to 1.42.5
1 parent 4d773fc commit ad99369

File tree

7 files changed

+51
-13
lines changed

7 files changed

+51
-13
lines changed

components/polylith/toml/core.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
from functools import lru_cache, reduce
44
from pathlib import Path
5-
from typing import List, Union
5+
from typing import Any, List, Union
66

77
import tomlkit
88
from polylith import repo
@@ -133,15 +133,32 @@ def parse_pep_621_dependency(dep: str) -> dict:
133133
return {name: version} if name else {}
134134

135135

136+
def _parse_poetry_complex_dependency(k: str, v: Any) -> dict:
137+
if not isinstance(v, dict):
138+
message = f"Unexpected dependency format. {k}: {v}"
139+
raise ValueError(message)
140+
141+
extras = sorted(v.get("extras") or [])
142+
version = v.get("version") or ""
143+
py_version = v.get("python")
144+
145+
python_version = str.replace(str(py_version), " ", "") if py_version else None
146+
147+
key = f"{k}-python{python_version}" if python_version else k
148+
name = key + str.replace(f"{extras}", "'", "") if extras else key
149+
150+
return {name: version}
151+
152+
136153
def parse_poetry_dependency(acc: dict, kv: tuple) -> dict:
137154
k, v = kv
138155

139156
if isinstance(v, dict):
140-
extras = sorted(v.get("extras", []))
141-
version = v.get("version", "")
157+
parsed = _parse_poetry_complex_dependency(k, v)
158+
elif isinstance(v, list):
159+
deps = [_parse_poetry_complex_dependency(k, i) for i in v]
142160

143-
name = k + str.replace(f"{extras}", "'", "") if extras else k
144-
parsed = {name: version}
161+
parsed = {k: v for dep in deps for k, v in dep.items()}
145162
else:
146163
parsed = {k: v}
147164

projects/hatch_polylith_bricks/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "hatch-polylith-bricks"
3-
version = "1.5.4"
3+
version = "1.5.5"
44
description = "Hatch build hook plugin for Polylith"
55
authors = ['David Vujic']
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

projects/pdm_polylith_bricks/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pdm-polylith-bricks"
3-
version = "1.3.6"
3+
version = "1.3.7"
44
description = "a PDM build hook for Polylith"
55
authors = ["David Vujic"]
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

projects/pdm_polylith_workspace/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pdm-polylith-workspace"
3-
version = "1.3.6"
3+
version = "1.3.7"
44
description = "a PDM build hook for a Polylith workspace"
55
homepage = "https://davidvujic.github.io/python-polylith-docs/"
66
repository = "https://github.com/davidvujic/python-polylith"

projects/poetry_polylith_plugin/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "poetry-polylith-plugin"
3-
version = "1.48.3"
3+
version = "1.48.4"
44
description = "A Poetry plugin that adds tooling support for the Polylith Architecture"
55
authors = ["David Vujic"]
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

projects/polylith_cli/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "polylith-cli"
3-
version = "1.42.4"
3+
version = "1.42.5"
44
description = "Python tooling support for the Polylith Architecture"
55
authors = ['David Vujic']
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"

test/components/polylith/toml/test_core.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@
5858
build-backend = "hatchling.build"
5959
6060
[project]
61-
dependencies = ["fastapi~=0.109.2", "uvicorn[standard]~=0.25.0", "tomlkit"]
61+
dependencies = ["fastapi~=0.109.2",
62+
"uvicorn[standard]~=0.25.0",
63+
"tomlkit",
64+
"typing_extensions<4.7; python_version > '3.9'"
65+
]
6266
6367
[project.optional-dependencies]
6468
dev = ["an-optional-lib==1.2.3", "another"]
@@ -71,6 +75,10 @@
7175
fastapi = "^0.110.0"
7276
uvicorn = {extras = ["standard"], version = "^0.27.1"}
7377
tomlkit = "*"
78+
typing_extensions = [
79+
{ version = "<4.14", python = ">=3.8,<3.9" },
80+
{ version = "*", python = ">=3.9" }
81+
]
7482
7583
an-optional-lib = {version = "1.2.3", optional = true}
7684
another = {optional = true}
@@ -144,23 +152,36 @@ def test_get_hatch_package_includes_from_default_when_in_both():
144152

145153

146154
def test_parse_pep_621_project_dependencies():
155+
expected_pep_621 = {
156+
**expected_dependencies,
157+
**{"typing_extensions": "<4.7; python_version > '3.9'"},
158+
}
147159
data = tomlkit.loads(pep_621_toml_deps)
148160

149161
res = toml.parse_project_dependencies(data)
150162

151-
assert res == expected_dependencies
163+
assert res == expected_pep_621
152164

153165

154166
def test_parse_poetry_project_dependencies():
155167
expected = {**expected_dependencies, **{"python": "^3.10"}}
168+
extra = {
169+
"typing_extensions-python>=3.8,<3.9": "<4.14",
170+
"typing_extensions-python>=3.9": "*",
171+
}
172+
173+
expected_poetry = {**expected, **extra}
174+
156175
data = tomlkit.loads(poetry_toml_deps)
157176

158177
res = toml.parse_project_dependencies(data)
159178

160-
assert res.keys() == expected.keys()
179+
assert res.keys() == expected_poetry.keys()
161180
assert res["fastapi"] == "^0.110.0"
162181
assert res["tomlkit"] == "*"
163182
assert res["an-optional-lib"] == "1.2.3"
183+
assert res["typing_extensions-python>=3.8,<3.9"] == "<4.14"
184+
assert res["typing_extensions-python>=3.9"] == "*"
164185

165186

166187
def test_collect_hatch_exclude_patterns() -> None:

0 commit comments

Comments
 (0)