Skip to content

Commit 629f124

Browse files
authored
Fix error handling when missing project name (#216)
* fix(project): get project name KeyError with details about the actual failed project file * bump versions in Poetry plugin, CLI, Hatch hook, PDM hooks
1 parent 2827012 commit 629f124

File tree

8 files changed

+66
-17
lines changed

8 files changed

+66
-17
lines changed

components/polylith/project/get.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66
from polylith import configuration, repo, toml
77

88

9-
def get_project_name(data) -> str:
10-
if repo.is_pep_621_ready(data):
11-
return data["project"]["name"]
9+
def get_project_name(toml_data) -> str:
10+
if repo.is_pep_621_ready(toml_data):
11+
return toml_data["project"]["name"]
1212

13-
return data["tool"]["poetry"]["name"]
13+
return toml_data["tool"]["poetry"]["name"]
14+
15+
16+
def get_project_name_from_toml(data: dict) -> str:
17+
try:
18+
return get_project_name(data["toml"])
19+
except KeyError as e:
20+
path = data["path"]
21+
22+
raise KeyError(f"Error in {path}") from e
1423

1524

1625
@lru_cache
@@ -47,7 +56,7 @@ def get_packages_for_projects(root: Path) -> List[dict]:
4756

4857
return [
4958
{
50-
"name": get_project_name(d["toml"]),
59+
"name": get_project_name_from_toml(d),
5160
"packages": toml.get_project_package_includes(namespace, d["toml"]),
5261
"path": d["path"],
5362
"type": d["type"],

components/polylith/toml/core.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ def get_project_package_includes(namespace: str, data) -> List[dict]:
5656
return [transform_to_package(namespace, key) for key in includes.keys()]
5757

5858

59-
def get_project_name(data) -> str:
60-
if repo.is_pep_621_ready(data):
61-
return data["project"]["name"]
62-
63-
return data["tool"]["poetry"]["name"]
64-
65-
6659
def parse_pep_621_dependency(dep: str) -> dict:
6760
parts = re.split(r"[\^~=!<>]", dep)
6861

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.2.3"
3+
version = "1.2.4"
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.0.4"
3+
version = "1.0.5"
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.0.4"
3+
version = "1.0.5"
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.20.0"
3+
version = "1.20.1"
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.7.0"
3+
version = "1.7.1"
44
description = "Python tooling support for the Polylith Architecture"
55
authors = ['David Vujic']
66
homepage = "https://davidvujic.github.io/python-polylith-docs/"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from pathlib import Path
2+
3+
import tomlkit
4+
from polylith.project import get
5+
import pytest
6+
7+
8+
poetry_toml = """\
9+
[tool.poetry]
10+
name = "unit-test"
11+
12+
[build-system]
13+
requires = ["poetry-core>=1.0.0"]
14+
build-backend = "poetry.core.masonry.api"
15+
"""
16+
17+
pep_621_toml = """\
18+
[project]
19+
name = "unit-test"
20+
21+
[build-system]
22+
requires = ["hatchling"]
23+
build-backend = "hatchling.build"
24+
"""
25+
26+
27+
def test_get_project_name_from_poetry_project():
28+
data = {"toml": tomlkit.loads(poetry_toml), "path": Path.cwd()}
29+
30+
assert get.get_project_name_from_toml(data) == "unit-test"
31+
32+
33+
def test_get_project_name_from_pep_621_project():
34+
data = {"toml": tomlkit.loads(pep_621_toml), "path": Path.cwd()}
35+
36+
assert get.get_project_name_from_toml(data) == "unit-test"
37+
38+
39+
def test_fetching_project_name_from_empty_project_raises_error():
40+
path = Path.cwd()
41+
42+
data = {"toml": tomlkit.loads(""), "path": path}
43+
44+
with pytest.raises(KeyError) as e:
45+
get.get_project_name_from_toml(data)
46+
47+
assert str(path) in str(e.value)

0 commit comments

Comments
 (0)