deproc-python 0.3.0__tar.gz → 0.4.1__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 (44) hide show
  1. deproc_python-0.4.1/PKG-INFO +128 -0
  2. deproc_python-0.4.1/README.md +104 -0
  3. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/linker/main.py +14 -1
  4. deproc_python-0.4.1/deproc/plugins/python/utils/packages.py +281 -0
  5. deproc_python-0.4.1/deproc_python.egg-info/PKG-INFO +128 -0
  6. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc_python.egg-info/SOURCES.txt +2 -0
  7. deproc_python-0.4.1/deproc_python.egg-info/requires.txt +4 -0
  8. {deproc_python-0.3.0 → deproc_python-0.4.1}/pyproject.toml +4 -3
  9. {deproc_python-0.3.0 → deproc_python-0.4.1}/tests/test_parser.py +1 -1
  10. deproc_python-0.4.1/tests/test_shared_registry_precedence.py +515 -0
  11. deproc_python-0.3.0/PKG-INFO +0 -22
  12. deproc_python-0.3.0/deproc/plugins/python/utils/packages.py +0 -115
  13. deproc_python-0.3.0/deproc_python.egg-info/PKG-INFO +0 -22
  14. deproc_python-0.3.0/deproc_python.egg-info/requires.txt +0 -4
  15. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/__init__.py +0 -0
  16. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/linker/__init__.py +0 -0
  17. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/linker/models.py +0 -0
  18. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/parser/__init__.py +0 -0
  19. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/parser/main.py +0 -0
  20. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/parser/models.py +0 -0
  21. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/parser/utils/extraction.py +0 -0
  22. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/parser/utils/misc.py +0 -0
  23. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/parser/utils/tree_sitter_python.py +0 -0
  24. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/resolver/__init__.py +0 -0
  25. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/resolver/main.py +0 -0
  26. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/resolver/models.py +0 -0
  27. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/symbol_cache/__init__.py +0 -0
  28. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/symbol_cache/main.py +0 -0
  29. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/symbol_cache/models.py +0 -0
  30. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/utils/exports.py +0 -0
  31. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/utils/imports.py +0 -0
  32. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/utils/mro.py +0 -0
  33. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc/plugins/python/utils/serialization.py +0 -0
  34. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc_python.egg-info/dependency_links.txt +0 -0
  35. {deproc_python-0.3.0 → deproc_python-0.4.1}/deproc_python.egg-info/top_level.txt +0 -0
  36. {deproc_python-0.3.0 → deproc_python-0.4.1}/setup.cfg +0 -0
  37. {deproc_python-0.3.0 → deproc_python-0.4.1}/tests/test_exports.py +0 -0
  38. {deproc_python-0.3.0 → deproc_python-0.4.1}/tests/test_imports.py +0 -0
  39. {deproc_python-0.3.0 → deproc_python-0.4.1}/tests/test_linker.py +0 -0
  40. {deproc_python-0.3.0 → deproc_python-0.4.1}/tests/test_mro.py +0 -0
  41. {deproc_python-0.3.0 → deproc_python-0.4.1}/tests/test_package_precedence.py +0 -0
  42. {deproc_python-0.3.0 → deproc_python-0.4.1}/tests/test_python_serialization.py +0 -0
  43. {deproc_python-0.3.0 → deproc_python-0.4.1}/tests/test_resolver.py +0 -0
  44. {deproc_python-0.3.0 → deproc_python-0.4.1}/tests/test_symbol_cache.py +0 -0
@@ -0,0 +1,128 @@
1
+ Metadata-Version: 2.4
2
+ Name: deproc-python
3
+ Version: 0.4.1
4
+ Summary: Python language plugin for deproc.
5
+ Author-email: Gurveer Singh Virk <gurveervirk.15517@gmail.com>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/gurveervirk/deproc
8
+ Project-URL: Repository, https://github.com/gurveervirk/deproc
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Topic :: Software Development :: Libraries
18
+ Requires-Python: >=3.12
19
+ Description-Content-Type: text/markdown
20
+ Requires-Dist: deproc-core>=0.4.0
21
+ Requires-Dist: deproc-utils-tree-sitter>=0.1.3
22
+ Requires-Dist: tree-sitter<0.26,>=0.25.2
23
+ Requires-Dist: tree-sitter-python>=0.25.0
24
+
25
+ # deproc-python
26
+
27
+ Python language plugin for [deproc](https://github.com/gurveervirk/deproc).
28
+
29
+ Parses `.py` and `.pyi` files with tree-sitter into the deproc entity model,
30
+ links them into a package hierarchy, and resolves names across modules.
31
+
32
+ ## Features
33
+
34
+ - Modules, classes, functions and methods, constants, type aliases, imports and
35
+ import aliases as deproc entities
36
+ - Regular packages (`__init__.py`) and implicit namespace packages, including a
37
+ namespace package contributed by several roots
38
+ - Python's regular-package precedence across roots: a regular package wins over
39
+ a same-named namespace portion from another root
40
+ - Name resolution through imports, relative imports and wildcard imports
41
+ - Class MRO (C3 linearization) and inherited members
42
+ - Entity serialization to flat records and back
43
+
44
+ ## Install
45
+
46
+ ```bash
47
+ python -m pip install deproc-python
48
+ ```
49
+
50
+ `deproc-python` supports Python 3.12 and newer and installs `deproc-core` with it.
51
+
52
+ ## Usage
53
+
54
+ ```python
55
+ from deproc.core.context import Context
56
+ from deproc.core.discovery import find_source_files
57
+ from deproc.plugins.python import (
58
+ PythonLinker,
59
+ PythonResolver,
60
+ PythonSourceParser,
61
+ PythonSymbolCache,
62
+ )
63
+
64
+ ctx = Context(base_path="/path/to/project")
65
+ ctx.set_language("python", [".py", ".pyi"], aliases=["py"])
66
+ ctx.set_parser("python", PythonSourceParser())
67
+ ctx.set_linker("python", PythonLinker())
68
+ ctx.set_resolver("python", PythonResolver())
69
+ ctx.set_symbol_cache(PythonSymbolCache())
70
+
71
+ files = find_source_files(ctx)
72
+ parser = ctx.get_parser("python")
73
+ modules = [parser.parse_file(path, ctx) for path in files]
74
+ ctx.get_linker("python").link_files(modules, ctx)
75
+
76
+ result = ctx.get_resolver("python").resolve("acme_billing.invoice", "TaxRate", ctx)
77
+ print(result.status) # resolved
78
+ for entity_id in result.resolved_ids:
79
+ print(ctx.entity_registry.get(entity_id).fqn) # acme_billing.tax.TaxRate
80
+ ```
81
+
82
+ `result.status` is a `ResolutionStatus` (`RESOLVED`, `UNRESOLVED`, `AMBIGUOUS`, or
83
+ `INACCESSIBLE`) from `deproc.core.interfaces.resolver`. The result also carries
84
+ `unresolved_ids`, `ambiguous_ids`, `inaccessible_ids`, `reason` and `candidates`.
85
+
86
+ `PythonResolver` also provides `resolve_class_mro(class_id, ctx)` and
87
+ `get_inherited_members(class_id, ctx)`.
88
+
89
+ ## Several roots
90
+
91
+ When several roots contribute the same package name, deproc follows Python's
92
+ import rule:
93
+
94
+ - A regular package (`__init__.py`) in one root wins over a same-named namespace
95
+ portion in another root, in any root order. The shadowed container is removed
96
+ and its modules stay indexed with no parent.
97
+ - Namespace portions with the same name in several roots become one namespace
98
+ package.
99
+ - Two regular packages with the same name in different roots are both kept, and a
100
+ warning names the roots, because Python would import whichever comes first on
101
+ its path.
102
+
103
+ `PythonLinker.link_files()` applies this rule automatically when roots share one
104
+ registry; `PythonLinker(apply_package_precedence=False)` turns it off.
105
+ `deproc.plugins.python.utils.packages` provides
106
+ `apply_regular_package_precedence()` for separate per-root registries,
107
+ `apply_regular_package_precedence_to_registry()` for one shared registry, and
108
+ `find_regular_package_conflicts()`. Pass `root_labels` to either precedence
109
+ function to name roots in the warning.
110
+
111
+ ## Package layout
112
+
113
+ ```
114
+ deproc-python/
115
+ ├── deproc/plugins/python/
116
+ │ ├── parser/ PythonSourceParser (tree-sitter AST → entities)
117
+ │ ├── linker/ PythonLinker (modules → package hierarchy)
118
+ │ ├── resolver/ PythonResolver (names, MRO, inherited members)
119
+ │ ├── symbol_cache/ PythonSymbolCache (per-module cache)
120
+ │ └── utils/ imports, exports, MRO, package precedence, serialization
121
+ └── tests/
122
+ ```
123
+
124
+ ## Dependencies
125
+
126
+ - `deproc-core` (entity model, Context, EntityRegistry)
127
+ - `deproc-utils-tree-sitter` (AST walking utilities)
128
+ - `tree-sitter` (`>=0.25.2,<0.26`) and `tree-sitter-python` (Python grammar)
@@ -0,0 +1,104 @@
1
+ # deproc-python
2
+
3
+ Python language plugin for [deproc](https://github.com/gurveervirk/deproc).
4
+
5
+ Parses `.py` and `.pyi` files with tree-sitter into the deproc entity model,
6
+ links them into a package hierarchy, and resolves names across modules.
7
+
8
+ ## Features
9
+
10
+ - Modules, classes, functions and methods, constants, type aliases, imports and
11
+ import aliases as deproc entities
12
+ - Regular packages (`__init__.py`) and implicit namespace packages, including a
13
+ namespace package contributed by several roots
14
+ - Python's regular-package precedence across roots: a regular package wins over
15
+ a same-named namespace portion from another root
16
+ - Name resolution through imports, relative imports and wildcard imports
17
+ - Class MRO (C3 linearization) and inherited members
18
+ - Entity serialization to flat records and back
19
+
20
+ ## Install
21
+
22
+ ```bash
23
+ python -m pip install deproc-python
24
+ ```
25
+
26
+ `deproc-python` supports Python 3.12 and newer and installs `deproc-core` with it.
27
+
28
+ ## Usage
29
+
30
+ ```python
31
+ from deproc.core.context import Context
32
+ from deproc.core.discovery import find_source_files
33
+ from deproc.plugins.python import (
34
+ PythonLinker,
35
+ PythonResolver,
36
+ PythonSourceParser,
37
+ PythonSymbolCache,
38
+ )
39
+
40
+ ctx = Context(base_path="/path/to/project")
41
+ ctx.set_language("python", [".py", ".pyi"], aliases=["py"])
42
+ ctx.set_parser("python", PythonSourceParser())
43
+ ctx.set_linker("python", PythonLinker())
44
+ ctx.set_resolver("python", PythonResolver())
45
+ ctx.set_symbol_cache(PythonSymbolCache())
46
+
47
+ files = find_source_files(ctx)
48
+ parser = ctx.get_parser("python")
49
+ modules = [parser.parse_file(path, ctx) for path in files]
50
+ ctx.get_linker("python").link_files(modules, ctx)
51
+
52
+ result = ctx.get_resolver("python").resolve("acme_billing.invoice", "TaxRate", ctx)
53
+ print(result.status) # resolved
54
+ for entity_id in result.resolved_ids:
55
+ print(ctx.entity_registry.get(entity_id).fqn) # acme_billing.tax.TaxRate
56
+ ```
57
+
58
+ `result.status` is a `ResolutionStatus` (`RESOLVED`, `UNRESOLVED`, `AMBIGUOUS`, or
59
+ `INACCESSIBLE`) from `deproc.core.interfaces.resolver`. The result also carries
60
+ `unresolved_ids`, `ambiguous_ids`, `inaccessible_ids`, `reason` and `candidates`.
61
+
62
+ `PythonResolver` also provides `resolve_class_mro(class_id, ctx)` and
63
+ `get_inherited_members(class_id, ctx)`.
64
+
65
+ ## Several roots
66
+
67
+ When several roots contribute the same package name, deproc follows Python's
68
+ import rule:
69
+
70
+ - A regular package (`__init__.py`) in one root wins over a same-named namespace
71
+ portion in another root, in any root order. The shadowed container is removed
72
+ and its modules stay indexed with no parent.
73
+ - Namespace portions with the same name in several roots become one namespace
74
+ package.
75
+ - Two regular packages with the same name in different roots are both kept, and a
76
+ warning names the roots, because Python would import whichever comes first on
77
+ its path.
78
+
79
+ `PythonLinker.link_files()` applies this rule automatically when roots share one
80
+ registry; `PythonLinker(apply_package_precedence=False)` turns it off.
81
+ `deproc.plugins.python.utils.packages` provides
82
+ `apply_regular_package_precedence()` for separate per-root registries,
83
+ `apply_regular_package_precedence_to_registry()` for one shared registry, and
84
+ `find_regular_package_conflicts()`. Pass `root_labels` to either precedence
85
+ function to name roots in the warning.
86
+
87
+ ## Package layout
88
+
89
+ ```
90
+ deproc-python/
91
+ ├── deproc/plugins/python/
92
+ │ ├── parser/ PythonSourceParser (tree-sitter AST → entities)
93
+ │ ├── linker/ PythonLinker (modules → package hierarchy)
94
+ │ ├── resolver/ PythonResolver (names, MRO, inherited members)
95
+ │ ├── symbol_cache/ PythonSymbolCache (per-module cache)
96
+ │ └── utils/ imports, exports, MRO, package precedence, serialization
97
+ └── tests/
98
+ ```
99
+
100
+ ## Dependencies
101
+
102
+ - `deproc-core` (entity model, Context, EntityRegistry)
103
+ - `deproc-utils-tree-sitter` (AST walking utilities)
104
+ - `tree-sitter` (`>=0.25.2,<0.26`) and `tree-sitter-python` (Python grammar)
@@ -6,10 +6,14 @@ from deproc.core.interfaces import Linker
6
6
  from deproc.core.interfaces.parser.models import SemanticContainer
7
7
 
8
8
  from ..parser.models import PythonModule
9
+ from ..utils.packages import apply_regular_package_precedence_to_registry
9
10
  from .models import Node, PythonNamespacePackage, PythonPackage
10
11
 
11
12
 
12
13
  class PythonLinker(Linker[PythonModule, Node | SemanticContainer]):
14
+ def __init__(self, *, apply_package_precedence: bool = True) -> None:
15
+ self.apply_package_precedence = apply_package_precedence
16
+
13
17
  def _check_skip_patterns(self, path: str, skip_patterns: set[str]) -> bool:
14
18
  return any(fnmatch.fnmatch(path, pattern) for pattern in skip_patterns)
15
19
 
@@ -47,7 +51,16 @@ class PythonLinker(Linker[PythonModule, Node | SemanticContainer]):
47
51
  sub_node.parent_id = None
48
52
  top_level.append(sub_node)
49
53
 
50
- return top_level
54
+ if not self.apply_package_precedence:
55
+ return top_level
56
+
57
+ apply_regular_package_precedence_to_registry(context.entity_registry)
58
+ return [
59
+ node
60
+ for node in top_level
61
+ if not isinstance(node, SemanticContainer)
62
+ or node.id in context.entity_registry
63
+ ]
51
64
 
52
65
  def validate_path(self, path: str) -> bool:
53
66
  return (
@@ -0,0 +1,281 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ from collections.abc import Mapping, 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
+ from ..parser.models import PythonModule
12
+
13
+ logger = logging.getLogger(__name__)
14
+
15
+
16
+ @dataclass(frozen=True)
17
+ class ShadowedNamespacePortion:
18
+ fqn: str
19
+ path: str
20
+ registry_index: int
21
+ detached_ids: tuple[SymbolID, ...]
22
+
23
+
24
+ @dataclass(frozen=True)
25
+ class RegularPackageConflict:
26
+ fqn: str
27
+ source_root_ids: tuple[str | None, ...]
28
+
29
+
30
+ def _shadows(regular_fqns: set[str], fqn: str) -> bool:
31
+ return any(
32
+ fqn == regular or fqn.startswith(f"{regular}.") for regular in regular_fqns
33
+ )
34
+
35
+
36
+ def _root_label(
37
+ root_id: str | None, root_labels: Mapping[str | None, str] | None
38
+ ) -> str:
39
+ if root_labels is not None and root_id in root_labels:
40
+ return root_labels[root_id]
41
+ return root_id if root_id is not None else "<no root id>"
42
+
43
+
44
+ def _log_shadowed(fqn: str, detached: int) -> None:
45
+ logger.warning(
46
+ "namespace portion '%s' is shadowed by a regular package of the "
47
+ "same name in another root; its %d module(s) stay indexed "
48
+ "without a parent package",
49
+ fqn,
50
+ detached,
51
+ )
52
+
53
+
54
+ def find_regular_package_conflicts(
55
+ registries: Sequence[EntityRegistry],
56
+ ) -> list[RegularPackageConflict]:
57
+ by_fqn: dict[str, dict[SymbolID, PythonPackage]] = {}
58
+ for registry in registries:
59
+ for entity in registry.values():
60
+ if isinstance(entity, PythonPackage):
61
+ by_fqn.setdefault(entity.fqn, {})[entity.id] = entity
62
+ return [
63
+ RegularPackageConflict(
64
+ fqn=fqn,
65
+ source_root_ids=tuple(
66
+ sorted(
67
+ (package.source_root_id for package in packages.values()),
68
+ key=lambda root_id: root_id or "",
69
+ )
70
+ ),
71
+ )
72
+ for fqn, packages in sorted(by_fqn.items())
73
+ if len(packages) > 1
74
+ ]
75
+
76
+
77
+ def _log_conflicts(
78
+ conflicts: Sequence[RegularPackageConflict],
79
+ root_labels: Mapping[str | None, str] | None,
80
+ ) -> None:
81
+ for conflict in conflicts:
82
+ logger.warning(
83
+ "regular package '%s' is defined in more than one root (%s); Python "
84
+ "imports only the first one on its path, and all of them stay indexed",
85
+ conflict.fqn,
86
+ ", ".join(
87
+ _root_label(root_id, root_labels)
88
+ for root_id in conflict.source_root_ids
89
+ ),
90
+ )
91
+
92
+
93
+ def apply_regular_package_precedence(
94
+ registries: Sequence[EntityRegistry],
95
+ *,
96
+ root_labels: Mapping[str | None, str] | None = None,
97
+ ) -> list[ShadowedNamespacePortion]:
98
+ """Apply Python's rule that a regular package wins over a namespace portion.
99
+
100
+ A namespace portion in one registry is shadowed when a different registry
101
+ holds a regular package (one with ``__init__.py``) of the same name, or of
102
+ an ancestor name. Python then never imports that portion, so its container
103
+ is removed and the modules it held stay in the registry without a parent.
104
+ A warning is logged for each regular package name that more than one
105
+ registry defines; all of them are kept.
106
+
107
+ Args:
108
+ registries: The per-root link results, before they are merged.
109
+ root_labels: Names for roots in the warning, by source root id.
110
+ Unlabelled roots show their id.
111
+
112
+ Returns:
113
+ The shadowed portions, with the module IDs each one detached.
114
+ """
115
+ _log_conflicts(find_regular_package_conflicts(registries), root_labels)
116
+ regular_by_registry = [
117
+ {
118
+ entity.fqn
119
+ for entity in registry.values()
120
+ if isinstance(entity, PythonPackage)
121
+ }
122
+ for registry in registries
123
+ ]
124
+ shadowed: list[ShadowedNamespacePortion] = []
125
+ for index, registry in enumerate(registries):
126
+ foreign_regular = {
127
+ fqn
128
+ for other, fqns in enumerate(regular_by_registry)
129
+ if other != index
130
+ for fqn in fqns
131
+ }
132
+ if not foreign_regular:
133
+ continue
134
+ candidates = sorted(
135
+ (
136
+ entity
137
+ for entity in registry.values()
138
+ if isinstance(entity, PythonNamespacePackage)
139
+ and _shadows(foreign_regular, entity.fqn)
140
+ ),
141
+ key=lambda entity: (entity.fqn, entity.path),
142
+ )
143
+ for container in candidates:
144
+ if container.id not in registry:
145
+ continue
146
+ detached = _drop_portion(registry, container)
147
+ portion = ShadowedNamespacePortion(
148
+ fqn=container.fqn,
149
+ path=container.path,
150
+ registry_index=index,
151
+ detached_ids=tuple(detached),
152
+ )
153
+ shadowed.append(portion)
154
+ _log_shadowed(container.fqn, len(detached))
155
+ return shadowed
156
+
157
+
158
+ def apply_regular_package_precedence_to_registry(
159
+ registry: EntityRegistry,
160
+ *,
161
+ root_labels: Mapping[str | None, str] | None = None,
162
+ ) -> list[ShadowedNamespacePortion]:
163
+ regular: dict[str, list[PythonPackage]] = {}
164
+ for entity in registry.values():
165
+ if isinstance(entity, PythonPackage):
166
+ regular.setdefault(entity.fqn, []).append(entity)
167
+ _log_conflicts(find_regular_package_conflicts([registry]), root_labels)
168
+ if not regular:
169
+ return []
170
+
171
+ containers = sorted(
172
+ (
173
+ entity
174
+ for entity in registry.values()
175
+ if isinstance(entity, PythonNamespacePackage)
176
+ and _shadows(set(regular), entity.fqn)
177
+ ),
178
+ key=lambda entity: (-entity.fqn.count("."), entity.fqn, entity.path),
179
+ )
180
+ shadowed: list[ShadowedNamespacePortion] = []
181
+ for container in containers:
182
+ if container.id not in registry:
183
+ continue
184
+ exact = container.fqn in regular
185
+ regular_roots = {
186
+ package.source_root_id
187
+ for fqn, packages in regular.items()
188
+ if _shadows({fqn}, container.fqn)
189
+ for package in packages
190
+ }
191
+ owner = (
192
+ min(
193
+ regular[container.fqn],
194
+ key=lambda package: (package.source_root_id or "", package.path),
195
+ )
196
+ if exact
197
+ else None
198
+ )
199
+ kept: list[SymbolID] = []
200
+ detached: list[SymbolID] = []
201
+ for child_id in container.submodule_ids:
202
+ child = registry.get(child_id)
203
+ if child is None:
204
+ continue
205
+ if isinstance(child, PythonNamespacePackage):
206
+ if owner is not None:
207
+ child.parent_id = owner.id
208
+ if child.id not in owner.submodule_ids:
209
+ owner.submodule_ids.append(child.id)
210
+ else:
211
+ kept.append(child_id)
212
+ continue
213
+ if (
214
+ owner is None
215
+ and isinstance(child, PythonModule)
216
+ and child.source_root_id in regular_roots
217
+ ):
218
+ kept.append(child_id)
219
+ continue
220
+ child.parent_id = None
221
+ detached.append(child.id)
222
+ removed = owner is not None or (bool(detached) and not kept)
223
+ if removed:
224
+ parent = registry.get(container.parent_id) if container.parent_id else None
225
+ if isinstance(parent, (PythonNamespacePackage, PythonPackage)):
226
+ parent.submodule_ids = [
227
+ entity_id
228
+ for entity_id in parent.submodule_ids
229
+ if entity_id != container.id
230
+ ]
231
+ registry.remove(container.id)
232
+ else:
233
+ container.submodule_ids = kept
234
+ if removed or detached:
235
+ shadowed.append(
236
+ ShadowedNamespacePortion(
237
+ fqn=container.fqn,
238
+ path=container.path,
239
+ registry_index=0,
240
+ detached_ids=tuple(sorted(detached)),
241
+ )
242
+ )
243
+ _log_shadowed(container.fqn, len(detached))
244
+ shadowed.sort(key=lambda portion: (portion.fqn, portion.path))
245
+ return shadowed
246
+
247
+
248
+ def _drop_portion(
249
+ registry: EntityRegistry, container: PythonNamespacePackage
250
+ ) -> list[SymbolID]:
251
+ detached: list[SymbolID] = []
252
+ pending = [container]
253
+ while pending:
254
+ current = pending.pop()
255
+ parent = registry.get(current.parent_id) if current.parent_id else None
256
+ if isinstance(parent, (PythonNamespacePackage, PythonPackage)):
257
+ parent.submodule_ids = [
258
+ entity_id
259
+ for entity_id in parent.submodule_ids
260
+ if entity_id != current.id
261
+ ]
262
+ registry.remove(current.id)
263
+ for child_id in current.submodule_ids:
264
+ child = registry.get(child_id)
265
+ if child is None:
266
+ continue
267
+ if isinstance(child, PythonNamespacePackage):
268
+ pending.append(child)
269
+ else:
270
+ child.parent_id = None
271
+ detached.append(child.id)
272
+ return sorted(detached)
273
+
274
+
275
+ __all__ = [
276
+ "RegularPackageConflict",
277
+ "ShadowedNamespacePortion",
278
+ "apply_regular_package_precedence",
279
+ "apply_regular_package_precedence_to_registry",
280
+ "find_regular_package_conflicts",
281
+ ]
@@ -0,0 +1,128 @@
1
+ Metadata-Version: 2.4
2
+ Name: deproc-python
3
+ Version: 0.4.1
4
+ Summary: Python language plugin for deproc.
5
+ Author-email: Gurveer Singh Virk <gurveervirk.15517@gmail.com>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/gurveervirk/deproc
8
+ Project-URL: Repository, https://github.com/gurveervirk/deproc
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Topic :: Software Development :: Libraries
18
+ Requires-Python: >=3.12
19
+ Description-Content-Type: text/markdown
20
+ Requires-Dist: deproc-core>=0.4.0
21
+ Requires-Dist: deproc-utils-tree-sitter>=0.1.3
22
+ Requires-Dist: tree-sitter<0.26,>=0.25.2
23
+ Requires-Dist: tree-sitter-python>=0.25.0
24
+
25
+ # deproc-python
26
+
27
+ Python language plugin for [deproc](https://github.com/gurveervirk/deproc).
28
+
29
+ Parses `.py` and `.pyi` files with tree-sitter into the deproc entity model,
30
+ links them into a package hierarchy, and resolves names across modules.
31
+
32
+ ## Features
33
+
34
+ - Modules, classes, functions and methods, constants, type aliases, imports and
35
+ import aliases as deproc entities
36
+ - Regular packages (`__init__.py`) and implicit namespace packages, including a
37
+ namespace package contributed by several roots
38
+ - Python's regular-package precedence across roots: a regular package wins over
39
+ a same-named namespace portion from another root
40
+ - Name resolution through imports, relative imports and wildcard imports
41
+ - Class MRO (C3 linearization) and inherited members
42
+ - Entity serialization to flat records and back
43
+
44
+ ## Install
45
+
46
+ ```bash
47
+ python -m pip install deproc-python
48
+ ```
49
+
50
+ `deproc-python` supports Python 3.12 and newer and installs `deproc-core` with it.
51
+
52
+ ## Usage
53
+
54
+ ```python
55
+ from deproc.core.context import Context
56
+ from deproc.core.discovery import find_source_files
57
+ from deproc.plugins.python import (
58
+ PythonLinker,
59
+ PythonResolver,
60
+ PythonSourceParser,
61
+ PythonSymbolCache,
62
+ )
63
+
64
+ ctx = Context(base_path="/path/to/project")
65
+ ctx.set_language("python", [".py", ".pyi"], aliases=["py"])
66
+ ctx.set_parser("python", PythonSourceParser())
67
+ ctx.set_linker("python", PythonLinker())
68
+ ctx.set_resolver("python", PythonResolver())
69
+ ctx.set_symbol_cache(PythonSymbolCache())
70
+
71
+ files = find_source_files(ctx)
72
+ parser = ctx.get_parser("python")
73
+ modules = [parser.parse_file(path, ctx) for path in files]
74
+ ctx.get_linker("python").link_files(modules, ctx)
75
+
76
+ result = ctx.get_resolver("python").resolve("acme_billing.invoice", "TaxRate", ctx)
77
+ print(result.status) # resolved
78
+ for entity_id in result.resolved_ids:
79
+ print(ctx.entity_registry.get(entity_id).fqn) # acme_billing.tax.TaxRate
80
+ ```
81
+
82
+ `result.status` is a `ResolutionStatus` (`RESOLVED`, `UNRESOLVED`, `AMBIGUOUS`, or
83
+ `INACCESSIBLE`) from `deproc.core.interfaces.resolver`. The result also carries
84
+ `unresolved_ids`, `ambiguous_ids`, `inaccessible_ids`, `reason` and `candidates`.
85
+
86
+ `PythonResolver` also provides `resolve_class_mro(class_id, ctx)` and
87
+ `get_inherited_members(class_id, ctx)`.
88
+
89
+ ## Several roots
90
+
91
+ When several roots contribute the same package name, deproc follows Python's
92
+ import rule:
93
+
94
+ - A regular package (`__init__.py`) in one root wins over a same-named namespace
95
+ portion in another root, in any root order. The shadowed container is removed
96
+ and its modules stay indexed with no parent.
97
+ - Namespace portions with the same name in several roots become one namespace
98
+ package.
99
+ - Two regular packages with the same name in different roots are both kept, and a
100
+ warning names the roots, because Python would import whichever comes first on
101
+ its path.
102
+
103
+ `PythonLinker.link_files()` applies this rule automatically when roots share one
104
+ registry; `PythonLinker(apply_package_precedence=False)` turns it off.
105
+ `deproc.plugins.python.utils.packages` provides
106
+ `apply_regular_package_precedence()` for separate per-root registries,
107
+ `apply_regular_package_precedence_to_registry()` for one shared registry, and
108
+ `find_regular_package_conflicts()`. Pass `root_labels` to either precedence
109
+ function to name roots in the warning.
110
+
111
+ ## Package layout
112
+
113
+ ```
114
+ deproc-python/
115
+ ├── deproc/plugins/python/
116
+ │ ├── parser/ PythonSourceParser (tree-sitter AST → entities)
117
+ │ ├── linker/ PythonLinker (modules → package hierarchy)
118
+ │ ├── resolver/ PythonResolver (names, MRO, inherited members)
119
+ │ ├── symbol_cache/ PythonSymbolCache (per-module cache)
120
+ │ └── utils/ imports, exports, MRO, package precedence, serialization
121
+ └── tests/
122
+ ```
123
+
124
+ ## Dependencies
125
+
126
+ - `deproc-core` (entity model, Context, EntityRegistry)
127
+ - `deproc-utils-tree-sitter` (AST walking utilities)
128
+ - `tree-sitter` (`>=0.25.2,<0.26`) and `tree-sitter-python` (Python grammar)
@@ -1,3 +1,4 @@
1
+ README.md
1
2
  pyproject.toml
2
3
  deproc/plugins/python/__init__.py
3
4
  deproc/plugins/python/linker/__init__.py
@@ -33,4 +34,5 @@ tests/test_package_precedence.py
33
34
  tests/test_parser.py
34
35
  tests/test_python_serialization.py
35
36
  tests/test_resolver.py
37
+ tests/test_shared_registry_precedence.py
36
38
  tests/test_symbol_cache.py
@@ -0,0 +1,4 @@
1
+ deproc-core>=0.4.0
2
+ deproc-utils-tree-sitter>=0.1.3
3
+ tree-sitter<0.26,>=0.25.2
4
+ tree-sitter-python>=0.25.0