deproc-python 0.3.0__py3-none-any.whl → 0.4.0__py3-none-any.whl

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.
@@ -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 (
@@ -8,6 +8,7 @@ from deproc.core.interfaces.parser.models import SymbolID
8
8
  from deproc.core.runtime import EntityRegistry
9
9
 
10
10
  from ..linker.models import PythonNamespacePackage, PythonPackage
11
+ from ..parser.models import PythonModule
11
12
 
12
13
  logger = logging.getLogger(__name__)
13
14
 
@@ -20,12 +21,65 @@ class ShadowedNamespacePortion:
20
21
  detached_ids: tuple[SymbolID, ...]
21
22
 
22
23
 
24
+ @dataclass(frozen=True)
25
+ class RegularPackageConflict:
26
+ fqn: str
27
+ source_root_ids: tuple[str | None, ...]
28
+
29
+
23
30
  def _shadows(regular_fqns: set[str], fqn: str) -> bool:
24
31
  return any(
25
32
  fqn == regular or fqn.startswith(f"{regular}.") for regular in regular_fqns
26
33
  )
27
34
 
28
35
 
36
+ def _root_label(root_id: str | None) -> str:
37
+ return root_id if root_id is not None else "<no root id>"
38
+
39
+
40
+ def _log_shadowed(fqn: str, detached: int) -> None:
41
+ logger.warning(
42
+ "namespace portion '%s' is shadowed by a regular package of the "
43
+ "same name in another root; its %d module(s) stay indexed "
44
+ "without a parent package",
45
+ fqn,
46
+ detached,
47
+ )
48
+
49
+
50
+ def find_regular_package_conflicts(
51
+ registries: Sequence[EntityRegistry],
52
+ ) -> list[RegularPackageConflict]:
53
+ by_fqn: dict[str, dict[SymbolID, PythonPackage]] = {}
54
+ for registry in registries:
55
+ for entity in registry.values():
56
+ if isinstance(entity, PythonPackage):
57
+ by_fqn.setdefault(entity.fqn, {})[entity.id] = entity
58
+ return [
59
+ RegularPackageConflict(
60
+ fqn=fqn,
61
+ source_root_ids=tuple(
62
+ sorted(
63
+ (package.source_root_id for package in packages.values()),
64
+ key=lambda root_id: root_id or "",
65
+ )
66
+ ),
67
+ )
68
+ for fqn, packages in sorted(by_fqn.items())
69
+ if len(packages) > 1
70
+ ]
71
+
72
+
73
+ def _log_conflicts(conflicts: Sequence[RegularPackageConflict]) -> None:
74
+ for conflict in conflicts:
75
+ logger.warning(
76
+ "regular package '%s' is defined in more than one root (%s); Python "
77
+ "imports only the first one on its path, and all of them stay indexed",
78
+ conflict.fqn,
79
+ ", ".join(_root_label(root_id) for root_id in conflict.source_root_ids),
80
+ )
81
+
82
+
29
83
  def apply_regular_package_precedence(
30
84
  registries: Sequence[EntityRegistry],
31
85
  ) -> list[ShadowedNamespacePortion]:
@@ -36,7 +90,10 @@ def apply_regular_package_precedence(
36
90
  holds a regular package (one with ``__init__.py``) of the same name, or of
37
91
  an ancestor name. Python then never imports that portion, so its container
38
92
  is removed and the modules it held stay in the registry without a parent.
93
+ A warning is logged for each regular package name that more than one
94
+ registry defines; all of them are kept.
39
95
  """
96
+ _log_conflicts(find_regular_package_conflicts(registries))
40
97
  regular_by_registry = [
41
98
  {
42
99
  entity.fqn
@@ -75,13 +132,95 @@ def apply_regular_package_precedence(
75
132
  detached_ids=tuple(detached),
76
133
  )
77
134
  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),
135
+ _log_shadowed(container.fqn, len(detached))
136
+ return shadowed
137
+
138
+
139
+ def apply_regular_package_precedence_to_registry(
140
+ registry: EntityRegistry,
141
+ ) -> list[ShadowedNamespacePortion]:
142
+ regular: dict[str, list[PythonPackage]] = {}
143
+ for entity in registry.values():
144
+ if isinstance(entity, PythonPackage):
145
+ regular.setdefault(entity.fqn, []).append(entity)
146
+ _log_conflicts(find_regular_package_conflicts([registry]))
147
+ if not regular:
148
+ return []
149
+
150
+ containers = sorted(
151
+ (
152
+ entity
153
+ for entity in registry.values()
154
+ if isinstance(entity, PythonNamespacePackage)
155
+ and _shadows(set(regular), entity.fqn)
156
+ ),
157
+ key=lambda entity: (-entity.fqn.count("."), entity.fqn, entity.path),
158
+ )
159
+ shadowed: list[ShadowedNamespacePortion] = []
160
+ for container in containers:
161
+ if container.id not in registry:
162
+ continue
163
+ exact = container.fqn in regular
164
+ regular_roots = {
165
+ package.source_root_id
166
+ for fqn, packages in regular.items()
167
+ if _shadows({fqn}, container.fqn)
168
+ for package in packages
169
+ }
170
+ owner = (
171
+ min(
172
+ regular[container.fqn],
173
+ key=lambda package: (package.source_root_id or "", package.path),
174
+ )
175
+ if exact
176
+ else None
177
+ )
178
+ kept: list[SymbolID] = []
179
+ detached: list[SymbolID] = []
180
+ for child_id in container.submodule_ids:
181
+ child = registry.get(child_id)
182
+ if child is None:
183
+ continue
184
+ if isinstance(child, PythonNamespacePackage):
185
+ if owner is not None:
186
+ child.parent_id = owner.id
187
+ if child.id not in owner.submodule_ids:
188
+ owner.submodule_ids.append(child.id)
189
+ else:
190
+ kept.append(child_id)
191
+ continue
192
+ if (
193
+ owner is None
194
+ and isinstance(child, PythonModule)
195
+ and child.source_root_id in regular_roots
196
+ ):
197
+ kept.append(child_id)
198
+ continue
199
+ child.parent_id = None
200
+ detached.append(child.id)
201
+ removed = owner is not None or (bool(detached) and not kept)
202
+ if removed:
203
+ parent = registry.get(container.parent_id) if container.parent_id else None
204
+ if isinstance(parent, (PythonNamespacePackage, PythonPackage)):
205
+ parent.submodule_ids = [
206
+ entity_id
207
+ for entity_id in parent.submodule_ids
208
+ if entity_id != container.id
209
+ ]
210
+ registry.remove(container.id)
211
+ else:
212
+ container.submodule_ids = kept
213
+ if removed or detached:
214
+ shadowed.append(
215
+ ShadowedNamespacePortion(
216
+ fqn=container.fqn,
217
+ path=container.path,
218
+ registry_index=0,
219
+ detached_ids=tuple(sorted(detached)),
220
+ )
84
221
  )
222
+ _log_shadowed(container.fqn, len(detached))
223
+ shadowed.sort(key=lambda portion: (portion.fqn, portion.path))
85
224
  return shadowed
86
225
 
87
226
 
@@ -112,4 +251,10 @@ def _drop_portion(
112
251
  return sorted(detached)
113
252
 
114
253
 
115
- __all__ = ["ShadowedNamespacePortion", "apply_regular_package_precedence"]
254
+ __all__ = [
255
+ "RegularPackageConflict",
256
+ "ShadowedNamespacePortion",
257
+ "apply_regular_package_precedence",
258
+ "apply_regular_package_precedence_to_registry",
259
+ "find_regular_package_conflicts",
260
+ ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deproc-python
3
- Version: 0.3.0
3
+ Version: 0.4.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
@@ -17,6 +17,6 @@ Classifier: Programming Language :: Python :: 3.13
17
17
  Classifier: Topic :: Software Development :: Libraries
18
18
  Requires-Python: >=3.12
19
19
  Requires-Dist: deproc-core>=0.4.0
20
- Requires-Dist: deproc-utils-tree-sitter>=0.1.2
21
- Requires-Dist: tree-sitter>=0.25.2
20
+ Requires-Dist: deproc-utils-tree-sitter>=0.1.3
21
+ Requires-Dist: tree-sitter<0.26,>=0.25.2
22
22
  Requires-Dist: tree-sitter-python>=0.25.0
@@ -1,6 +1,6 @@
1
1
  deproc/plugins/python/__init__.py,sha256=JuMffHnYtF82voXTOP4m1hQ-zTGA5OeroWhHuz9v8nk,504
2
2
  deproc/plugins/python/linker/__init__.py,sha256=gQ64CQcNEDHfvyAuYAz81PZ6r2V0ZdnsVOR2YZZvvfU,59
3
- deproc/plugins/python/linker/main.py,sha256=As8K2k1PtxdO7GnFC4cRQXRDzVJOFWqKPLx5O_xj2V4,3641
3
+ deproc/plugins/python/linker/main.py,sha256=cdaqf4MmaMWiR_7pgSTFy4j8chQs8je3GK0_ouTSGWA,4167
4
4
  deproc/plugins/python/linker/models.py,sha256=aXigj6ATiTmM_wQVjSQP_Te8An6wm_XhR3InauqRaMc,1198
5
5
  deproc/plugins/python/parser/__init__.py,sha256=dcfU6uyIOBUOvnz9xM4hX6gc0ldTGavHZCSIv6lbWWk,71
6
6
  deproc/plugins/python/parser/main.py,sha256=hXODQpsCTOnTmi4xlTbGhl95SyusU5RmYMFZm5obtCI,31197
@@ -17,9 +17,9 @@ deproc/plugins/python/symbol_cache/models.py,sha256=1SgqUyhNnwZcITRCL8a-21T26niQ
17
17
  deproc/plugins/python/utils/exports.py,sha256=45dvBfClVEo_ZRN6vCPEx0VH3-eRQiKahv1Qgg074Lo,5218
18
18
  deproc/plugins/python/utils/imports.py,sha256=DWvcN2SdYmhQgRl9RYAc5nxbbijSEtAJ11X_Xrf3x_U,547
19
19
  deproc/plugins/python/utils/mro.py,sha256=6tTC5lHFK2PZWX-454dGKNmiVhFDrMRQhI0bgDJZlrk,1565
20
- deproc/plugins/python/utils/packages.py,sha256=d6gUP1_KE8_r3StPpdRpH5TyWJnaMlpXVqzBOFEm1HI,3861
20
+ deproc/plugins/python/utils/packages.py,sha256=Tdedjc8c7_R9rK9KWtZAOjNuqu55uM3ZRPwId1rsEU4,8857
21
21
  deproc/plugins/python/utils/serialization.py,sha256=oiuezQ5XslWBPnQyCidEPHxqOSkBfFS0xFQ6PVsQ-ic,16903
22
- deproc_python-0.3.0.dist-info/METADATA,sha256=UVBExd7lUnv4FpWAe2MuGPP9BdIfdEAG8ERmorq5JjQ,949
23
- deproc_python-0.3.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
24
- deproc_python-0.3.0.dist-info/top_level.txt,sha256=qyPIAnJVTLaewuAbLpShmn-777EtCI7b1b5sGgkzO10,7
25
- deproc_python-0.3.0.dist-info/RECORD,,
22
+ deproc_python-0.4.0.dist-info/METADATA,sha256=xda6brcG4Bsbj-bnHPx2mudvR98YDvObK6DjF-Rxokg,955
23
+ deproc_python-0.4.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
24
+ deproc_python-0.4.0.dist-info/top_level.txt,sha256=qyPIAnJVTLaewuAbLpShmn-777EtCI7b1b5sGgkzO10,7
25
+ deproc_python-0.4.0.dist-info/RECORD,,