Skip to content

Commit

Permalink
feat(poly check, poly libs): include optional third party deps (#207)
Browse files Browse the repository at this point in the history
* feat(parsing third-party deps): include optional dependencies for PEP 621 compliant configs

* bump hatch build hook to 1.2.2

* bump pdm brick build hook to 1.0.2

* bump pdm workspace build hook to 1.0.2

* bump Poetry plugin to 1.19.0

* bump cli to 1.6.0
  • Loading branch information
DavidVujic committed May 1, 2024
1 parent 6d49440 commit ed51bd9
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 11 deletions.
12 changes: 11 additions & 1 deletion components/polylith/toml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def parse_poetry_dependency(acc: dict, kv: tuple) -> dict:
return {**acc, **parsed}


def get_pep_621_optional_dependencies(data) -> List[str]:
groups = data["project"].get("optional-dependencies", {})
matrix = [v for v in groups.values()] if isinstance(groups, dict) else []

return sum(matrix, [])


def parse_project_dependencies(data) -> dict:
if repo.is_poetry(data):
deps = data["tool"]["poetry"].get("dependencies", {})
Expand All @@ -95,8 +102,11 @@ def parse_project_dependencies(data) -> dict:
return res

deps = data["project"].get("dependencies", [])
optional_deps = get_pep_621_optional_dependencies(data)

all_deps = deps + optional_deps

return {k: v for dep in deps for k, v in parse_pep_621_dependency(dep).items()}
return {k: v for dep in all_deps for k, v in parse_pep_621_dependency(dep).items()}


def get_project_dependencies(data) -> dict:
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.2.1"
version = "1.2.2"
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.0.1"
version = "1.0.2"
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.0.1"
version = "1.0.2"
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.18.3"
version = "1.19.0"
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.5.4"
version = "1.6.0"
description = "Python tooling support for the Polylith Architecture"
authors = ['David Vujic']
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Expand Down
74 changes: 69 additions & 5 deletions test/components/polylith/toml/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,36 @@
"../../components/unittest/two" = "unittest/two"
"""

expected = [
pep_621_toml_deps = """\
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
dependencies = ["fastapi~=0.109.2", "uvicorn~=0.25.0", "tomlkit"]
[project.optional-dependencies]
dev = ["an-optional-lib==1.2.3", "another"]
local = ["awsglue-local-dev==1.0.0"]
"""

poetry_toml_deps = """\
[tool.poetry.dependencies]
python = "^3.10"
fastapi = "^0.110.0"
uvicorn = {extras = ["standard"], version = "^0.27.1"}
tomlkit = "*"
an-optional-lib = {version = "1.2.3", optional = true}
another = {optional = true}
awsglue-local-dev = {version = "1.0.0", optional = true}
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
"""

expected_packages = [
{"include": "unittest/one", "from": "../../bases"},
{"include": "unittest/two", "from": "../../components"},
]
Expand All @@ -62,28 +91,63 @@ def test_get_poetry_package_includes():

res = toml.get_project_package_includes(namespace, data)

assert res == expected
assert res == expected_packages


def test_get_hatch_package_includes():
data = tomlkit.loads(hatch_toml)

res = toml.get_project_package_includes(namespace, data)

assert res == expected
assert res == expected_packages


def test_get_hatch_package_includes_in_build_hook():
data = tomlkit.loads(hatch_toml_alternative)

res = toml.get_project_package_includes(namespace, data)

assert res == expected
assert res == expected_packages


def test_get_hatch_package_includes_from_default_when_in_both():
data = tomlkit.loads(hatch_toml_combined)

res = toml.get_project_package_includes(namespace, data)

assert res == expected
assert res == expected_packages


def test_parse_pep_621_project_dependencies():
expected_dependencies = {
"fastapi": "~=0.109.2",
"uvicorn": "~=0.25.0",
"tomlkit": "",
"an-optional-lib": "==1.2.3",
"another": "",
"awsglue-local-dev": "==1.0.0",
}

data = tomlkit.loads(pep_621_toml_deps)

res = toml.parse_project_dependencies(data)

assert res == expected_dependencies


def test_parse_poetry_project_dependencies():
expected_dependencies = {
"python": "^3.10",
"fastapi": "^0.110.0",
"uvicorn[standard]": "^0.27.1",
"tomlkit": "*",
"an-optional-lib": "1.2.3",
"another": "",
"awsglue-local-dev": "1.0.0",
}

data = tomlkit.loads(poetry_toml_deps)

res = toml.parse_project_dependencies(data)

assert res == expected_dependencies

0 comments on commit ed51bd9

Please sign in to comment.