Skip to content

Commit 70d8b9f

Browse files
authored
fix(poly check, poly libs): extract imports with similar top namespaces (#432)
* fix(poly check, poly libs): extract brick imports, and separate bricks from third-party libs with similar top namespace * bump Poetry plugin to 1.49.1 * bump CLI to 1.43.1
1 parent ece7e33 commit 70d8b9f

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

components/polylith/imports/grouping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def only_brick_imports(imports: Set[str], top_ns: str) -> Set[str]:
5-
return {i for i in imports if i.startswith(top_ns)}
5+
return {i for i in imports if i == top_ns or i.startswith(f"{top_ns}.")}
66

77

88
def only_bricks(import_data: dict, top_ns: str) -> dict:
@@ -29,7 +29,7 @@ def exclude_empty(import_data: dict) -> dict:
2929
return {k: v for k, v in import_data.items() if v}
3030

3131

32-
def extract_brick_imports(all_imports: dict, top_ns) -> dict:
32+
def extract_brick_imports(all_imports: dict, top_ns: str) -> dict:
3333
with_only_bricks = only_bricks(all_imports, top_ns)
3434
with_only_brick_names = only_brick_names(with_only_bricks)
3535

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.49.0"
3+
version = "1.49.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.43.0"
3+
version = "1.43.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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from polylith.imports import (
2+
extract_brick_imports,
3+
extract_brick_imports_with_namespaces,
4+
)
5+
6+
top_ns = "my_ns"
7+
8+
all_imports = {
9+
"brick_a": {"hello.world", f"{top_ns}.first", "typing.Set"},
10+
"brick_b": {"httpx.Request"},
11+
"brick_c": {f"{top_ns}.second"},
12+
"brick_d": {top_ns},
13+
}
14+
15+
16+
def test_extract_brick_imports() -> None:
17+
res = extract_brick_imports(all_imports, top_ns)
18+
19+
assert res == {
20+
"brick_a": {"first"},
21+
"brick_c": {"second"},
22+
}
23+
24+
25+
def test_extract_brick_imports_when_third_party_starts_with_top_ns() -> None:
26+
extra = {"brick_x": {f"{top_ns}_something_else.Request"}}
27+
28+
combined = {**all_imports, **extra}
29+
res = extract_brick_imports(combined, top_ns)
30+
31+
assert res == {
32+
"brick_a": {"first"},
33+
"brick_c": {"second"},
34+
}
35+
36+
37+
def test_extract_brick_imports_with_ns() -> None:
38+
res = extract_brick_imports_with_namespaces(all_imports, top_ns)
39+
40+
assert res == {
41+
"brick_a": {f"{top_ns}.first"},
42+
"brick_c": {f"{top_ns}.second"},
43+
"brick_d": {top_ns},
44+
}
45+
46+
47+
def test_extract_brick_imports_with_ns_when_third_party_starts_with_top_ns() -> None:
48+
extra = {"brick_x": {f"{top_ns}_something_else.Request"}}
49+
50+
combined = {**all_imports, **extra}
51+
res = extract_brick_imports_with_namespaces(combined, top_ns)
52+
53+
assert res == {
54+
"brick_a": {f"{top_ns}.first"},
55+
"brick_c": {f"{top_ns}.second"},
56+
"brick_d": {top_ns},
57+
}

0 commit comments

Comments
 (0)