Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions components/polylith/toml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from functools import lru_cache, reduce
from pathlib import Path
from typing import List, Union
from typing import Any, List, Union

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


def _parse_poetry_complex_dependency(k: str, v: Any) -> dict:
if not isinstance(v, dict):
message = f"Unexpected dependency format. {k}: {v}"
raise ValueError(message)

extras = sorted(v.get("extras") or [])
version = v.get("version") or ""
py_version = v.get("python")

python_version = str.replace(str(py_version), " ", "") if py_version else None

key = f"{k}-python{python_version}" if python_version else k
name = key + str.replace(f"{extras}", "'", "") if extras else key

return {name: version}


def parse_poetry_dependency(acc: dict, kv: tuple) -> dict:
k, v = kv

if isinstance(v, dict):
extras = sorted(v.get("extras", []))
version = v.get("version", "")
parsed = _parse_poetry_complex_dependency(k, v)
elif isinstance(v, list):
deps = [_parse_poetry_complex_dependency(k, i) for i in v]

name = k + str.replace(f"{extras}", "'", "") if extras else k
parsed = {name: version}
parsed = {k: v for dep in deps for k, v in dep.items()}
else:
parsed = {k: v}

Expand Down
2 changes: 1 addition & 1 deletion projects/hatch_polylith_bricks/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hatch-polylith-bricks"
version = "1.5.4"
version = "1.5.5"
description = "Hatch build hook plugin for Polylith"
authors = ['David Vujic']
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
2 changes: 1 addition & 1 deletion projects/pdm_polylith_bricks/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pdm-polylith-bricks"
version = "1.3.6"
version = "1.3.7"
description = "a PDM build hook for Polylith"
authors = ["David Vujic"]
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
2 changes: 1 addition & 1 deletion projects/pdm_polylith_workspace/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pdm-polylith-workspace"
version = "1.3.6"
version = "1.3.7"
description = "a PDM build hook for a Polylith workspace"
homepage = "https://davidvujic.github.io/python-polylith-docs/"
repository = "https://github.com/davidvujic/python-polylith"
Expand Down
2 changes: 1 addition & 1 deletion projects/poetry_polylith_plugin/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poetry-polylith-plugin"
version = "1.48.3"
version = "1.48.4"
description = "A Poetry plugin that adds tooling support for the Polylith Architecture"
authors = ["David Vujic"]
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
2 changes: 1 addition & 1 deletion projects/polylith_cli/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "polylith-cli"
version = "1.42.4"
version = "1.42.5"
description = "Python tooling support for the Polylith Architecture"
authors = ['David Vujic']
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
27 changes: 24 additions & 3 deletions test/components/polylith/toml/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@
build-backend = "hatchling.build"

[project]
dependencies = ["fastapi~=0.109.2", "uvicorn[standard]~=0.25.0", "tomlkit"]
dependencies = ["fastapi~=0.109.2",
"uvicorn[standard]~=0.25.0",
"tomlkit",
"typing_extensions<4.7; python_version > '3.9'"
]

[project.optional-dependencies]
dev = ["an-optional-lib==1.2.3", "another"]
Expand All @@ -71,6 +75,10 @@
fastapi = "^0.110.0"
uvicorn = {extras = ["standard"], version = "^0.27.1"}
tomlkit = "*"
typing_extensions = [
{ version = "<4.14", python = ">=3.8,<3.9" },
{ version = "*", python = ">=3.9" }
]

an-optional-lib = {version = "1.2.3", optional = true}
another = {optional = true}
Expand Down Expand Up @@ -144,23 +152,36 @@ def test_get_hatch_package_includes_from_default_when_in_both():


def test_parse_pep_621_project_dependencies():
expected_pep_621 = {
**expected_dependencies,
**{"typing_extensions": "<4.7; python_version > '3.9'"},
}
data = tomlkit.loads(pep_621_toml_deps)

res = toml.parse_project_dependencies(data)

assert res == expected_dependencies
assert res == expected_pep_621


def test_parse_poetry_project_dependencies():
expected = {**expected_dependencies, **{"python": "^3.10"}}
extra = {
"typing_extensions-python>=3.8,<3.9": "<4.14",
"typing_extensions-python>=3.9": "*",
}

expected_poetry = {**expected, **extra}

data = tomlkit.loads(poetry_toml_deps)

res = toml.parse_project_dependencies(data)

assert res.keys() == expected.keys()
assert res.keys() == expected_poetry.keys()
assert res["fastapi"] == "^0.110.0"
assert res["tomlkit"] == "*"
assert res["an-optional-lib"] == "1.2.3"
assert res["typing_extensions-python>=3.8,<3.9"] == "<4.14"
assert res["typing_extensions-python>=3.9"] == "*"


def test_collect_hatch_exclude_patterns() -> None:
Expand Down