deproc-python 0.2.1__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. {deproc_python-0.2.1 → deproc_python-0.3.0}/PKG-INFO +2 -2
  2. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/linker/main.py +12 -5
  3. deproc_python-0.3.0/deproc/plugins/python/linker/models.py +46 -0
  4. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/parser/main.py +1 -0
  5. deproc_python-0.3.0/deproc/plugins/python/utils/packages.py +115 -0
  6. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/utils/serialization.py +5 -0
  7. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc_python.egg-info/PKG-INFO +2 -2
  8. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc_python.egg-info/SOURCES.txt +2 -0
  9. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc_python.egg-info/requires.txt +1 -1
  10. {deproc_python-0.2.1 → deproc_python-0.3.0}/pyproject.toml +2 -2
  11. {deproc_python-0.2.1 → deproc_python-0.3.0}/tests/test_linker.py +117 -1
  12. deproc_python-0.3.0/tests/test_package_precedence.py +130 -0
  13. {deproc_python-0.2.1 → deproc_python-0.3.0}/tests/test_python_serialization.py +20 -1
  14. deproc_python-0.2.1/deproc/plugins/python/linker/models.py +0 -26
  15. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/__init__.py +0 -0
  16. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/linker/__init__.py +0 -0
  17. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/parser/__init__.py +0 -0
  18. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/parser/models.py +0 -0
  19. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/parser/utils/extraction.py +0 -0
  20. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/parser/utils/misc.py +0 -0
  21. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/parser/utils/tree_sitter_python.py +0 -0
  22. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/resolver/__init__.py +0 -0
  23. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/resolver/main.py +0 -0
  24. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/resolver/models.py +0 -0
  25. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/symbol_cache/__init__.py +0 -0
  26. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/symbol_cache/main.py +0 -0
  27. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/symbol_cache/models.py +0 -0
  28. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/utils/exports.py +0 -0
  29. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/utils/imports.py +0 -0
  30. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc/plugins/python/utils/mro.py +0 -0
  31. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc_python.egg-info/dependency_links.txt +0 -0
  32. {deproc_python-0.2.1 → deproc_python-0.3.0}/deproc_python.egg-info/top_level.txt +0 -0
  33. {deproc_python-0.2.1 → deproc_python-0.3.0}/setup.cfg +0 -0
  34. {deproc_python-0.2.1 → deproc_python-0.3.0}/tests/test_exports.py +0 -0
  35. {deproc_python-0.2.1 → deproc_python-0.3.0}/tests/test_imports.py +0 -0
  36. {deproc_python-0.2.1 → deproc_python-0.3.0}/tests/test_mro.py +0 -0
  37. {deproc_python-0.2.1 → deproc_python-0.3.0}/tests/test_parser.py +0 -0
  38. {deproc_python-0.2.1 → deproc_python-0.3.0}/tests/test_resolver.py +0 -0
  39. {deproc_python-0.2.1 → deproc_python-0.3.0}/tests/test_symbol_cache.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deproc-python
3
- Version: 0.2.1
3
+ Version: 0.3.0
4
4
  Summary: Python language plugin for deproc.
5
5
  Author-email: Gurveer Singh Virk <gurveervirk.15517@gmail.com>
6
6
  License: Apache-2.0
@@ -16,7 +16,7 @@ Classifier: Programming Language :: Python :: 3.12
16
16
  Classifier: Programming Language :: Python :: 3.13
17
17
  Classifier: Topic :: Software Development :: Libraries
18
18
  Requires-Python: >=3.12
19
- Requires-Dist: deproc-core>=0.2.0
19
+ Requires-Dist: deproc-core>=0.4.0
20
20
  Requires-Dist: deproc-utils-tree-sitter>=0.1.2
21
21
  Requires-Dist: tree-sitter>=0.25.2
22
22
  Requires-Dist: tree-sitter-python>=0.25.0
@@ -3,16 +3,19 @@ from pathlib import Path
3
3
 
4
4
  from deproc.core.context import Context
5
5
  from deproc.core.interfaces import Linker
6
+ from deproc.core.interfaces.parser.models import SemanticContainer
6
7
 
7
8
  from ..parser.models import PythonModule
8
9
  from .models import Node, PythonNamespacePackage, PythonPackage
9
10
 
10
11
 
11
- class PythonLinker(Linker[PythonModule, Node]):
12
+ class PythonLinker(Linker[PythonModule, Node | SemanticContainer]):
12
13
  def _check_skip_patterns(self, path: str, skip_patterns: set[str]) -> bool:
13
14
  return any(fnmatch.fnmatch(path, pattern) for pattern in skip_patterns)
14
15
 
15
- def link_files(self, nodes: list[PythonModule], context: Context) -> list[Node]:
16
+ def link_files(
17
+ self, nodes: list[PythonModule], context: Context
18
+ ) -> list[Node | SemanticContainer]:
16
19
  base_path = Path(context.base_path)
17
20
 
18
21
  path_to_module: dict[str, PythonModule] = {}
@@ -29,7 +32,7 @@ class PythonLinker(Linker[PythonModule, Node]):
29
32
  path_to_module[relative_path] = node
30
33
 
31
34
  skip_patterns = context.skip_paths
32
- top_level: list[Node] = []
35
+ top_level: list[Node | SemanticContainer] = []
33
36
  for sub_path in base_path.iterdir():
34
37
  if sub_path.name == "__pycache__":
35
38
  continue
@@ -61,7 +64,7 @@ class PythonLinker(Linker[PythonModule, Node]):
61
64
  path_to_module: dict[str, PythonModule],
62
65
  has_init: dict[str, PythonModule],
63
66
  context: Context,
64
- ) -> Node | None:
67
+ ) -> Node | SemanticContainer | None:
65
68
  if not self.validate_path(str(current_path)):
66
69
  return None
67
70
 
@@ -94,5 +97,9 @@ class PythonLinker(Linker[PythonModule, Node]):
94
97
  sub_node.parent_id = package.id
95
98
  package.submodule_ids.append(sub_node.id)
96
99
 
97
- context.entity_registry.add(package)
100
+ if isinstance(package, SemanticContainer):
101
+ package.submodule_ids = sorted(set(package.submodule_ids))
102
+ package = context.entity_registry.merge_semantic_container(package)
103
+ else:
104
+ context.entity_registry.add(package)
98
105
  return package
@@ -0,0 +1,46 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from deproc.core.interfaces.parser.models import Node, SemanticContainer
4
+
5
+ from ..parser.models import (
6
+ PythonModule,
7
+ SymbolID,
8
+ )
9
+
10
+
11
+ @dataclass
12
+ class PythonNamespacePackage(SemanticContainer):
13
+ path: str
14
+ fqn: str
15
+ submodule_ids: list[SymbolID] = field(default_factory=list)
16
+
17
+ @property
18
+ def semantic_type(self) -> str:
19
+ return "python.namespace-package"
20
+
21
+ def __post_init__(self) -> None:
22
+ super().__post_init__()
23
+ self.submodule_ids = sorted(set(self.submodule_ids))
24
+
25
+ @property
26
+ def contribution_ids(self) -> list[SymbolID]:
27
+ return self.submodule_ids
28
+
29
+ def _merge_contributions(self, other: SemanticContainer) -> None:
30
+ if not isinstance(other, PythonNamespacePackage):
31
+ raise ValueError(
32
+ "Namespace packages can only merge with namespace packages"
33
+ )
34
+ self.submodule_ids = sorted(set(self.submodule_ids) | set(other.submodule_ids))
35
+
36
+
37
+ @dataclass(kw_only=True)
38
+ class PythonPackage(PythonModule):
39
+ submodule_ids: list[SymbolID] = field(default_factory=list)
40
+
41
+
42
+ __all__ = [
43
+ "Node",
44
+ "PythonNamespacePackage",
45
+ "PythonPackage",
46
+ ]
@@ -91,6 +91,7 @@ class PythonSourceParser(SourceParser):
91
91
  source_file = PythonModule(
92
92
  fqn=parent_fqn,
93
93
  path=relative_path,
94
+ source_root_id=context.source_root_id,
94
95
  docstring_range=docstring_range,
95
96
  source=source_bytes.decode("utf-8"),
96
97
  )
@@ -0,0 +1,115 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ from collections.abc import Sequence
5
+ from dataclasses import dataclass
6
+
7
+ from deproc.core.interfaces.parser.models import SymbolID
8
+ from deproc.core.runtime import EntityRegistry
9
+
10
+ from ..linker.models import PythonNamespacePackage, PythonPackage
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+
15
+ @dataclass(frozen=True)
16
+ class ShadowedNamespacePortion:
17
+ fqn: str
18
+ path: str
19
+ registry_index: int
20
+ detached_ids: tuple[SymbolID, ...]
21
+
22
+
23
+ def _shadows(regular_fqns: set[str], fqn: str) -> bool:
24
+ return any(
25
+ fqn == regular or fqn.startswith(f"{regular}.") for regular in regular_fqns
26
+ )
27
+
28
+
29
+ def apply_regular_package_precedence(
30
+ registries: Sequence[EntityRegistry],
31
+ ) -> list[ShadowedNamespacePortion]:
32
+ """Apply Python's rule that a regular package wins over a namespace portion.
33
+
34
+ ``registries`` are the per-root link results, before they are merged. A
35
+ namespace portion in one registry is shadowed when a different registry
36
+ holds a regular package (one with ``__init__.py``) of the same name, or of
37
+ an ancestor name. Python then never imports that portion, so its container
38
+ is removed and the modules it held stay in the registry without a parent.
39
+ """
40
+ regular_by_registry = [
41
+ {
42
+ entity.fqn
43
+ for entity in registry.values()
44
+ if isinstance(entity, PythonPackage)
45
+ }
46
+ for registry in registries
47
+ ]
48
+ shadowed: list[ShadowedNamespacePortion] = []
49
+ for index, registry in enumerate(registries):
50
+ foreign_regular = {
51
+ fqn
52
+ for other, fqns in enumerate(regular_by_registry)
53
+ if other != index
54
+ for fqn in fqns
55
+ }
56
+ if not foreign_regular:
57
+ continue
58
+ candidates = sorted(
59
+ (
60
+ entity
61
+ for entity in registry.values()
62
+ if isinstance(entity, PythonNamespacePackage)
63
+ and _shadows(foreign_regular, entity.fqn)
64
+ ),
65
+ key=lambda entity: (entity.fqn, entity.path),
66
+ )
67
+ for container in candidates:
68
+ if container.id not in registry:
69
+ continue
70
+ detached = _drop_portion(registry, container)
71
+ portion = ShadowedNamespacePortion(
72
+ fqn=container.fqn,
73
+ path=container.path,
74
+ registry_index=index,
75
+ detached_ids=tuple(detached),
76
+ )
77
+ shadowed.append(portion)
78
+ logger.warning(
79
+ "namespace portion '%s' is shadowed by a regular package of the "
80
+ "same name in another root; its %d module(s) stay indexed "
81
+ "without a parent package",
82
+ container.fqn,
83
+ len(detached),
84
+ )
85
+ return shadowed
86
+
87
+
88
+ def _drop_portion(
89
+ registry: EntityRegistry, container: PythonNamespacePackage
90
+ ) -> list[SymbolID]:
91
+ detached: list[SymbolID] = []
92
+ pending = [container]
93
+ while pending:
94
+ current = pending.pop()
95
+ parent = registry.get(current.parent_id) if current.parent_id else None
96
+ if isinstance(parent, (PythonNamespacePackage, PythonPackage)):
97
+ parent.submodule_ids = [
98
+ entity_id
99
+ for entity_id in parent.submodule_ids
100
+ if entity_id != current.id
101
+ ]
102
+ registry.remove(current.id)
103
+ for child_id in current.submodule_ids:
104
+ child = registry.get(child_id)
105
+ if child is None:
106
+ continue
107
+ if isinstance(child, PythonNamespacePackage):
108
+ pending.append(child)
109
+ else:
110
+ child.parent_id = None
111
+ detached.append(child.id)
112
+ return sorted(detached)
113
+
114
+
115
+ __all__ = ["ShadowedNamespacePortion", "apply_regular_package_precedence"]
@@ -190,6 +190,9 @@ def entity_to_record(
190
190
  path = getattr(entity, "path", None)
191
191
  if path is not None:
192
192
  metadata["path"] = path
193
+ source_root_id = getattr(entity, "source_root_id", None)
194
+ if source_root_id:
195
+ metadata["source_root_id"] = source_root_id
193
196
  if isinstance(entity, PythonModule) and entity.all_exports is not None:
194
197
  metadata["all_exports"] = entity.all_exports
195
198
  if isinstance(entity, PythonModule) and entity.exports_dynamic:
@@ -344,6 +347,7 @@ def record_to_entity(record: dict) -> Entity | None:
344
347
  all_exports=meta.get("all_exports"),
345
348
  exports_dynamic=meta.get("exports_dynamic", False),
346
349
  path=meta.get("path", ""),
350
+ source_root_id=meta.get("source_root_id"),
347
351
  source="",
348
352
  docstring_range=None,
349
353
  import_stmt_ids=meta.get("import_stmt_ids", []),
@@ -360,6 +364,7 @@ def record_to_entity(record: dict) -> Entity | None:
360
364
  all_exports=meta.get("all_exports"),
361
365
  exports_dynamic=meta.get("exports_dynamic", False),
362
366
  path=meta.get("path", ""),
367
+ source_root_id=meta.get("source_root_id"),
363
368
  source="",
364
369
  docstring_range=None,
365
370
  import_stmt_ids=meta.get("import_stmt_ids", []),
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deproc-python
3
- Version: 0.2.1
3
+ Version: 0.3.0
4
4
  Summary: Python language plugin for deproc.
5
5
  Author-email: Gurveer Singh Virk <gurveervirk.15517@gmail.com>
6
6
  License: Apache-2.0
@@ -16,7 +16,7 @@ Classifier: Programming Language :: Python :: 3.12
16
16
  Classifier: Programming Language :: Python :: 3.13
17
17
  Classifier: Topic :: Software Development :: Libraries
18
18
  Requires-Python: >=3.12
19
- Requires-Dist: deproc-core>=0.2.0
19
+ Requires-Dist: deproc-core>=0.4.0
20
20
  Requires-Dist: deproc-utils-tree-sitter>=0.1.2
21
21
  Requires-Dist: tree-sitter>=0.25.2
22
22
  Requires-Dist: tree-sitter-python>=0.25.0
@@ -18,6 +18,7 @@ deproc/plugins/python/symbol_cache/models.py
18
18
  deproc/plugins/python/utils/exports.py
19
19
  deproc/plugins/python/utils/imports.py
20
20
  deproc/plugins/python/utils/mro.py
21
+ deproc/plugins/python/utils/packages.py
21
22
  deproc/plugins/python/utils/serialization.py
22
23
  deproc_python.egg-info/PKG-INFO
23
24
  deproc_python.egg-info/SOURCES.txt
@@ -28,6 +29,7 @@ tests/test_exports.py
28
29
  tests/test_imports.py
29
30
  tests/test_linker.py
30
31
  tests/test_mro.py
32
+ tests/test_package_precedence.py
31
33
  tests/test_parser.py
32
34
  tests/test_python_serialization.py
33
35
  tests/test_resolver.py
@@ -1,4 +1,4 @@
1
- deproc-core>=0.2.0
1
+ deproc-core>=0.4.0
2
2
  deproc-utils-tree-sitter>=0.1.2
3
3
  tree-sitter>=0.25.2
4
4
  tree-sitter-python>=0.25.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "deproc-python"
7
- version = "0.2.1"
7
+ version = "0.3.0"
8
8
  description = "Python language plugin for deproc."
9
9
  authors = [
10
10
  { name = "Gurveer Singh Virk", email = "gurveervirk.15517@gmail.com" }
@@ -12,7 +12,7 @@ authors = [
12
12
  license = {text = "Apache-2.0"}
13
13
  requires-python = ">=3.12"
14
14
  dependencies = [
15
- "deproc-core>=0.2.0",
15
+ "deproc-core>=0.4.0",
16
16
  "deproc-utils-tree-sitter>=0.1.2",
17
17
  "tree-sitter>=0.25.2",
18
18
  "tree-sitter-python>=0.25.0",
@@ -4,9 +4,16 @@ from pathlib import Path
4
4
  from unittest.mock import MagicMock
5
5
 
6
6
  from deproc.core.context import Context
7
+ from deproc.core.interfaces.parser.models import Node
7
8
  from deproc.core.runtime import EntityRegistry
8
9
  from deproc.plugins.python.linker.main import PythonLinker
9
- from deproc.plugins.python.parser.models import PythonModule
10
+ from deproc.plugins.python.linker.models import PythonNamespacePackage
11
+ from deproc.plugins.python.parser.main import PythonSourceParser
12
+ from deproc.plugins.python.parser.models import (
13
+ PythonClass,
14
+ PythonFunctionLike,
15
+ PythonModule,
16
+ )
10
17
 
11
18
 
12
19
  def _write_file(path: Path, content: str = "") -> Path:
@@ -190,3 +197,112 @@ class TestLinker:
190
197
  assert "dist" not in fqns
191
198
  assert "node_modules" not in fqns
192
199
  assert "src.egg-info" not in fqns
200
+
201
+ def test_namespace_package_aggregates_multiple_roots_independent_of_order(
202
+ self, tmp_path: Path
203
+ ):
204
+ roots = [
205
+ (
206
+ tmp_path / "root-a",
207
+ "root-a",
208
+ "pkg/a.py",
209
+ "class A:\n def run(self): pass\n",
210
+ ),
211
+ (
212
+ tmp_path / "root-b",
213
+ "root-b",
214
+ "pkg/b.py",
215
+ "class B:\n def run(self): pass\n",
216
+ ),
217
+ ]
218
+ for root, _, relative_path, source in roots:
219
+ _write_file(root / relative_path, source)
220
+
221
+ def link_in_order(ordered_roots):
222
+ registry = EntityRegistry()
223
+ for root, root_id, relative_path, _ in ordered_roots:
224
+ context = _make_context(str(root))
225
+ context.source_root_id = root_id
226
+ context.entity_registry = registry
227
+ module = PythonSourceParser().parse_file(
228
+ str(root / relative_path), context
229
+ )
230
+ PythonLinker().link_files([module], context)
231
+ packages = [
232
+ entity
233
+ for entity in registry.values()
234
+ if isinstance(entity, PythonNamespacePackage)
235
+ ]
236
+ return registry, packages
237
+
238
+ forward_registry, forward_packages = link_in_order(roots)
239
+ reverse_registry, reverse_packages = link_in_order(list(reversed(roots)))
240
+
241
+ assert len(forward_packages) == len(reverse_packages) == 1
242
+ forward = forward_packages[0]
243
+ reverse = reverse_packages[0]
244
+ assert forward.fqn == reverse.fqn == "pkg"
245
+ assert forward.id == reverse.id
246
+ assert forward.id != Node(path="pkg").id
247
+ assert forward.submodule_ids == reverse.submodule_ids
248
+ assert len(forward.submodule_ids) == 2
249
+ assert not hasattr(forward, "source_root_id")
250
+
251
+ for registry in (forward_registry, reverse_registry):
252
+ modules = [
253
+ entity
254
+ for entity in registry.values()
255
+ if isinstance(entity, PythonModule)
256
+ ]
257
+ classes = [
258
+ entity
259
+ for entity in registry.values()
260
+ if isinstance(entity, PythonClass)
261
+ ]
262
+ methods = [
263
+ entity
264
+ for entity in registry.values()
265
+ if isinstance(entity, PythonFunctionLike) and entity.type == "METHOD"
266
+ ]
267
+ assert len(modules) == len(classes) == len(methods) == 2
268
+ assert {module.id for module in modules} == set(forward.submodule_ids)
269
+ assert {entity.id for entity in forward_registry.values()} == {
270
+ entity.id for entity in reverse_registry.values()
271
+ }
272
+
273
+ def test_identical_source_path_in_two_roots_keeps_descendant_ids_distinct(
274
+ self, tmp_path: Path
275
+ ):
276
+ source = "class Shared:\n def run(self): pass\n"
277
+ registry = EntityRegistry()
278
+ modules = []
279
+ for root_name, root_id in (("root-a", "root-a"), ("root-b", "root-b")):
280
+ root = tmp_path / root_name
281
+ source_path = _write_file(root / "pkg" / "mod.py", source)
282
+ context = _make_context(str(root))
283
+ context.source_root_id = root_id
284
+ context.entity_registry = registry
285
+ module = PythonSourceParser().parse_file(str(source_path), context)
286
+ PythonLinker().link_files([module], context)
287
+ modules.append(module)
288
+
289
+ classes = [
290
+ entity for entity in registry.values() if isinstance(entity, PythonClass)
291
+ ]
292
+ methods = [
293
+ entity
294
+ for entity in registry.values()
295
+ if isinstance(entity, PythonFunctionLike) and entity.type == "METHOD"
296
+ ]
297
+ namespace = next(
298
+ entity
299
+ for entity in registry.values()
300
+ if isinstance(entity, PythonNamespacePackage)
301
+ )
302
+
303
+ assert modules[0].path == modules[1].path == "pkg/mod.py"
304
+ assert modules[0].source == modules[1].source
305
+ assert modules[0].id != modules[1].id
306
+ assert len({entity.id for entity in classes}) == 2
307
+ assert len({entity.id for entity in methods}) == 2
308
+ assert set(namespace.submodule_ids) == {module.id for module in modules}
@@ -0,0 +1,130 @@
1
+ from pathlib import Path
2
+
3
+ from deproc.core.context import Context
4
+ from deproc.core.runtime import EntityRegistry
5
+ from deproc.plugins.python.linker.main import PythonLinker
6
+ from deproc.plugins.python.linker.models import PythonNamespacePackage, PythonPackage
7
+ from deproc.plugins.python.parser.main import PythonSourceParser
8
+ from deproc.plugins.python.parser.models import PythonModule
9
+ from deproc.plugins.python.utils.packages import apply_regular_package_precedence
10
+
11
+
12
+ def _link_root(tmp_path: Path, name: str, files: dict[str, str]) -> EntityRegistry:
13
+ root = tmp_path / name
14
+ for relative_path, source in files.items():
15
+ path = root / relative_path
16
+ path.parent.mkdir(parents=True, exist_ok=True)
17
+ path.write_text(source)
18
+ context = Context(base_path=str(root))
19
+ context.source_root_id = name
20
+ context.entity_registry = EntityRegistry()
21
+ modules = [
22
+ PythonSourceParser().parse_file(str(root / relative_path), context)
23
+ for relative_path in sorted(files)
24
+ ]
25
+ PythonLinker().link_files(modules, context)
26
+ return context.entity_registry
27
+
28
+
29
+ def _module(registry: EntityRegistry, fqn: str) -> PythonModule:
30
+ (entity_id,) = registry.get_ids_by_fqn(fqn)
31
+ entity = registry.get(entity_id)
32
+ assert isinstance(entity, PythonModule)
33
+ return entity
34
+
35
+
36
+ REGULAR = {"ns/__init__.py": "", "ns/a.py": "class A: pass\n"}
37
+ NAMESPACE = {"ns/b.py": "class B: pass\n"}
38
+
39
+
40
+ def test_regular_package_wins_over_namespace_portion_in_either_order(tmp_path):
41
+ for order in ("regular-first", "namespace-first"):
42
+ regular = _link_root(tmp_path / order, "regular", REGULAR)
43
+ namespace = _link_root(tmp_path / order, "namespace", NAMESPACE)
44
+ registries = (
45
+ [regular, namespace] if order == "regular-first" else [namespace, regular]
46
+ )
47
+
48
+ shadowed = apply_regular_package_precedence(registries)
49
+
50
+ assert [portion.fqn for portion in shadowed] == ["ns"]
51
+ assert not any(
52
+ isinstance(entity, PythonNamespacePackage)
53
+ for registry in registries
54
+ for entity in registry.values()
55
+ )
56
+ assert _module(namespace, "ns.b").parent_id is None
57
+ assert shadowed[0].detached_ids == (_module(namespace, "ns.b").id,)
58
+ regular_package = next(
59
+ entity for entity in regular.values() if isinstance(entity, PythonPackage)
60
+ )
61
+ assert _module(regular, "ns.a").parent_id == regular_package.id
62
+
63
+
64
+ def test_shadowed_portion_is_removed_before_the_registries_are_merged(tmp_path):
65
+ regular = _link_root(tmp_path, "regular", REGULAR)
66
+ namespace = _link_root(tmp_path, "namespace", NAMESPACE)
67
+
68
+ apply_regular_package_precedence([regular, namespace])
69
+ merged = EntityRegistry()
70
+ merged.merge_from(regular)
71
+ merged.merge_from(namespace)
72
+
73
+ assert len(merged.get_ids_by_fqn("ns")) == 1
74
+ (package,) = [merged.get(entity_id) for entity_id in merged.get_ids_by_fqn("ns")]
75
+ assert isinstance(package, PythonPackage)
76
+ assert package.submodule_ids == [_module(regular, "ns.a").id]
77
+ assert _module(merged, "ns.b").parent_id is None
78
+
79
+
80
+ def test_namespace_portions_without_a_regular_package_are_left_alone(tmp_path):
81
+ first = _link_root(tmp_path, "first", {"ns/a.py": "class A: pass\n"})
82
+ second = _link_root(tmp_path, "second", NAMESPACE)
83
+
84
+ assert apply_regular_package_precedence([first, second]) == []
85
+
86
+ merged = EntityRegistry()
87
+ merged.merge_from(first)
88
+ merged.merge_from(second)
89
+ (namespace,) = [merged.get(entity_id) for entity_id in merged.get_ids_by_fqn("ns")]
90
+ assert isinstance(namespace, PythonNamespacePackage)
91
+ assert len(namespace.submodule_ids) == 2
92
+
93
+
94
+ def test_namespace_subpackage_inside_the_regular_root_is_kept(tmp_path):
95
+ regular = _link_root(
96
+ tmp_path,
97
+ "regular",
98
+ {"ns/__init__.py": "", "ns/sub/x.py": "class X: pass\n"},
99
+ )
100
+ namespace = _link_root(tmp_path, "namespace", {"ns/sub/y.py": "class Y: pass\n"})
101
+
102
+ shadowed = apply_regular_package_precedence([regular, namespace])
103
+
104
+ assert [portion.fqn for portion in shadowed] == ["ns"]
105
+ assert shadowed[0].registry_index == 1
106
+ assert shadowed[0].detached_ids == (_module(namespace, "ns.sub.y").id,)
107
+ assert not any(
108
+ isinstance(entity, PythonNamespacePackage) for entity in namespace.values()
109
+ )
110
+ kept = [
111
+ entity
112
+ for entity in regular.values()
113
+ if isinstance(entity, PythonNamespacePackage)
114
+ ]
115
+ assert [entity.fqn for entity in kept] == ["ns.sub"]
116
+ regular_package = next(
117
+ entity for entity in regular.values() if isinstance(entity, PythonPackage)
118
+ )
119
+ assert kept[0].parent_id == regular_package.id
120
+ assert kept[0].submodule_ids == [_module(regular, "ns.sub.x").id]
121
+ assert _module(namespace, "ns.sub.y").parent_id is None
122
+
123
+
124
+ def test_registries_without_foreign_regular_packages_are_untouched(tmp_path):
125
+ only = _link_root(tmp_path, "only", {**REGULAR, "other/m.py": "x = 1\n"})
126
+ before = {entity.id: entity.parent_id for entity in only.values()}
127
+
128
+ assert apply_regular_package_precedence([only]) == []
129
+
130
+ assert {entity.id: entity.parent_id for entity in only.values()} == before
@@ -1,7 +1,7 @@
1
1
  from deproc.core.context import Context
2
2
  from deproc.core.interfaces.parser.models import SourceRange
3
3
  from deproc.core.runtime.registries.entity import EntityRegistry
4
- from deproc.plugins.python.linker.models import PythonPackage
4
+ from deproc.plugins.python.linker.models import PythonNamespacePackage, PythonPackage
5
5
  from deproc.plugins.python.parser.models import (
6
6
  PythonClass,
7
7
  PythonConstant,
@@ -57,6 +57,7 @@ def test_serialize_round_trip_restores_module_ownership_and_exports():
57
57
  id="mod-id",
58
58
  fqn="pkg",
59
59
  path="pkg/__init__.py",
60
+ source_root_id="generated",
60
61
  source="",
61
62
  docstring_range=None,
62
63
  import_stmt_ids=[f"imp{i}" for i in range(3)],
@@ -74,6 +75,7 @@ def test_serialize_round_trip_restores_module_ownership_and_exports():
74
75
 
75
76
  assert isinstance(entity, PythonModule)
76
77
  assert entity.fqn == "pkg"
78
+ assert entity.source_root_id == "generated"
77
79
  assert entity.import_stmt_ids == ["imp0", "imp1", "imp2"]
78
80
  assert entity.type_ids == ["type0", "type1"]
79
81
  assert entity.function_ids == ["fn0", "fn1"]
@@ -137,6 +139,23 @@ def test_serialize_round_trip_restores_package_submodules():
137
139
  assert entity.import_stmt_ids == ["imp1"]
138
140
 
139
141
 
142
+ def test_serialize_round_trip_restores_namespace_package_contributions():
143
+ package = PythonNamespacePackage(
144
+ fqn="pkg",
145
+ path="pkg",
146
+ submodule_ids=["module-a", "module-b"],
147
+ )
148
+ record = entity_to_record(package)
149
+ assert record is not None
150
+
151
+ entity = record_to_entity(record)
152
+
153
+ assert isinstance(entity, PythonNamespacePackage)
154
+ assert entity.id == package.id
155
+ assert entity.semantic_key == "pkg"
156
+ assert entity.contribution_ids == ["module-a", "module-b"]
157
+
158
+
140
159
  def test_semantic_queries_survive_round_trip():
141
160
  source_range = SourceRange(lineno=1, end_lineno=1, col_offset=0, end_col_offset=1)
142
161
  base_module = PythonModule(
@@ -1,26 +0,0 @@
1
- from dataclasses import dataclass, field
2
-
3
- from deproc.core.interfaces.parser.models import Node
4
-
5
- from ..parser.models import (
6
- PythonModule,
7
- SymbolID,
8
- )
9
-
10
-
11
- @dataclass
12
- class PythonNamespacePackage(Node):
13
- fqn: str
14
- submodule_ids: list[SymbolID] = field(default_factory=list)
15
-
16
-
17
- @dataclass(kw_only=True)
18
- class PythonPackage(PythonModule):
19
- submodule_ids: list[SymbolID] = field(default_factory=list)
20
-
21
-
22
- __all__ = [
23
- "Node",
24
- "PythonNamespacePackage",
25
- "PythonPackage",
26
- ]
File without changes