deproc-python 0.2.0__py3-none-any.whl → 0.3.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.
@@ -1,9 +1,19 @@
1
1
  from .linker import PythonLinker
2
2
  from .parser import PythonSourceParser
3
- from .resolver import PythonResolver
3
+ from .resolver import (
4
+ PythonBaseResolution,
5
+ PythonClassMROResult,
6
+ PythonInheritedMember,
7
+ PythonInheritedMembersResult,
8
+ PythonResolver,
9
+ )
4
10
  from .symbol_cache import PythonSymbolCache
5
11
 
6
12
  __all__ = [
13
+ "PythonBaseResolution",
14
+ "PythonClassMROResult",
15
+ "PythonInheritedMember",
16
+ "PythonInheritedMembersResult",
7
17
  "PythonLinker",
8
18
  "PythonResolver",
9
19
  "PythonSourceParser",
@@ -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
@@ -1,6 +1,6 @@
1
1
  from dataclasses import dataclass, field
2
2
 
3
- from deproc.core.interfaces.parser.models import Node
3
+ from deproc.core.interfaces.parser.models import Node, SemanticContainer
4
4
 
5
5
  from ..parser.models import (
6
6
  PythonModule,
@@ -9,10 +9,30 @@ from ..parser.models import (
9
9
 
10
10
 
11
11
  @dataclass
12
- class PythonNamespacePackage(Node):
12
+ class PythonNamespacePackage(SemanticContainer):
13
+ path: str
13
14
  fqn: str
14
15
  submodule_ids: list[SymbolID] = field(default_factory=list)
15
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
+
16
36
 
17
37
  @dataclass(kw_only=True)
18
38
  class PythonPackage(PythonModule):
@@ -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
  )
@@ -1,3 +1,15 @@
1
1
  from .main import PythonResolver
2
+ from .models import (
3
+ PythonBaseResolution,
4
+ PythonClassMROResult,
5
+ PythonInheritedMember,
6
+ PythonInheritedMembersResult,
7
+ )
2
8
 
3
- __all__ = ["PythonResolver"]
9
+ __all__ = [
10
+ "PythonBaseResolution",
11
+ "PythonClassMROResult",
12
+ "PythonInheritedMember",
13
+ "PythonInheritedMembersResult",
14
+ "PythonResolver",
15
+ ]
@@ -1,8 +1,9 @@
1
1
  import logging
2
+ from collections.abc import Mapping
2
3
 
3
4
  from deproc.core.context import Context
4
5
  from deproc.core.interfaces.parser.models import Entity
5
- from deproc.core.interfaces.resolver import Resolver
6
+ from deproc.core.interfaces.resolver import ResolutionStatus, Resolver
6
7
 
7
8
  from ..parser.models import (
8
9
  PythonClass,
@@ -13,8 +14,12 @@ from ..parser.models import (
13
14
  )
14
15
  from ..utils.exports import build_module_exports, get_dynamic_export_modules
15
16
  from ..utils.imports import resolve_relative_import_path
16
- from ..utils.mro import compute_mro_from_bases
17
+ from ..utils.mro import compute_mro_from_base_ids
17
18
  from .models import (
19
+ PythonBaseResolution,
20
+ PythonClassMROResult,
21
+ PythonInheritedMember,
22
+ PythonInheritedMembersResult,
18
23
  PythonResolverResult,
19
24
  ResolvedIDs,
20
25
  UnresolvedIDs,
@@ -318,100 +323,345 @@ class PythonResolver(Resolver[PythonResolverResult]):
318
323
  )
319
324
  return resolved_ids, unresolved_ids
320
325
 
321
- def _resolve_base_ids(self, cls: PythonClass, context: Context) -> list[SymbolID]:
326
+ def resolve_import_alias(
327
+ self, alias_id: SymbolID, context: Context
328
+ ) -> PythonResolverResult:
329
+ """Resolve one imported binding without reinterpreting its import syntax."""
330
+ alias = self._get_symbol(alias_id, context)
331
+ if not isinstance(alias, PythonImportAlias):
332
+ return PythonResolverResult(
333
+ resolved_ids=set(),
334
+ unresolved_ids={alias_id},
335
+ reason=f"entity {alias_id} is not a Python import alias",
336
+ )
337
+
338
+ resolved_ids, unresolved_ids = self.resolve_alias_ids({alias_id}, context)
339
+ ambiguous_ids = resolved_ids if len(resolved_ids) > 1 else set()
340
+ if ambiguous_ids:
341
+ reason = f"Multiple symbols found for imported binding '{alias.name}'"
342
+ elif not resolved_ids:
343
+ reason = f"No symbol found for imported binding '{alias.name}'"
344
+ else:
345
+ reason = None
346
+ return PythonResolverResult(
347
+ resolved_ids=resolved_ids,
348
+ unresolved_ids=unresolved_ids,
349
+ ambiguous_ids=ambiguous_ids,
350
+ reason=reason,
351
+ )
352
+
353
+ @staticmethod
354
+ def _normalize_base_name(base_name: str) -> str:
355
+ return base_name.split("[", 1)[0].strip()
356
+
357
+ def _resolve_qualified_base_name(
358
+ self,
359
+ cls: PythonClass,
360
+ qualified_name: str,
361
+ context: Context,
362
+ ) -> tuple[ResolvedIDs, UnresolvedIDs]:
322
363
  module = self._get_module(cls.id, context)
323
364
  if module is None:
324
- return []
365
+ return set(), set()
325
366
 
326
- base_ids: list[SymbolID] = []
327
- for base_name in cls.inherits:
328
- base_name = base_name.split("[", 1)[0].strip()
329
- if not base_name:
367
+ components = qualified_name.split(".")
368
+ resolved_ids, unresolved_ids = self.resolve_symbol(
369
+ module.fqn, components[0], context
370
+ )
371
+ binding_prefix_length = 1
372
+ for binding_id in self.get_ids_by_fqn(module.fqn, components[0], context):
373
+ binding = self._get_symbol(binding_id, context)
374
+ if not isinstance(binding, PythonImportAlias) or binding.alias is not None:
375
+ continue
376
+ if binding.parent_id is None:
377
+ continue
378
+ import_statement = self._get_symbol(binding.parent_id, context)
379
+ if not isinstance(import_statement, PythonImportStatement):
380
+ continue
381
+ if import_statement.type != "generic_import":
382
+ continue
383
+ target_fqn = binding.import_path or self._get_target_module_fqn(
384
+ import_statement.id, context
385
+ )
386
+ if not target_fqn:
330
387
  continue
331
- resolved_ids = context.entity_registry.get_ids_by_fqn(base_name)
388
+ target_components = target_fqn.split(".")
389
+ if components[: len(target_components)] == target_components:
390
+ binding_prefix_length = max(
391
+ binding_prefix_length, len(target_components)
392
+ )
393
+
394
+ for component in components[binding_prefix_length:]:
395
+ next_resolved_ids: ResolvedIDs = set()
396
+ next_unresolved_ids: UnresolvedIDs = set(unresolved_ids)
397
+ for container_id in resolved_ids:
398
+ container = self._get_symbol(container_id, context)
399
+ container_fqn = getattr(container, "fqn", None)
400
+ if not container_fqn:
401
+ continue
402
+ resolved, unresolved = self.resolve_symbol(
403
+ container_fqn, component, context
404
+ )
405
+ next_resolved_ids.update(resolved)
406
+ next_unresolved_ids.update(unresolved)
407
+ resolved_ids = next_resolved_ids
408
+ unresolved_ids = next_unresolved_ids
332
409
  if not resolved_ids:
333
- if "." in base_name:
334
- base_module, base_symbol = base_name.rsplit(".", 1)
335
- resolved_ids, _ = self.resolve_symbol(
336
- base_module, base_symbol, context
337
- )
338
- else:
339
- resolved_ids, _ = self.resolve_symbol(
340
- module.fqn, base_name, context
410
+ break
411
+
412
+ return resolved_ids, unresolved_ids
413
+
414
+ def _resolve_base(
415
+ self,
416
+ cls: PythonClass,
417
+ base_name: str,
418
+ context: Context,
419
+ base_overrides: Mapping[tuple[SymbolID, str], SymbolID] | None = None,
420
+ ) -> PythonBaseResolution:
421
+ normalized_name = self._normalize_base_name(base_name)
422
+ override_id = (base_overrides or {}).get((cls.id, normalized_name))
423
+ if override_id:
424
+ override = self._get_symbol(override_id, context)
425
+ if isinstance(override, PythonClass):
426
+ return PythonBaseResolution(
427
+ name=base_name,
428
+ status=ResolutionStatus.RESOLVED,
429
+ resolved_id=override_id,
430
+ candidates=(override_id,),
431
+ )
432
+
433
+ unresolved_ids: set[SymbolID] = set()
434
+ if "." in normalized_name:
435
+ resolved_ids, unresolved_ids = self._resolve_qualified_base_name(
436
+ cls, normalized_name, context
437
+ )
438
+ else:
439
+ resolved_ids = context.entity_registry.get_ids_by_fqn(normalized_name)
440
+ if not resolved_ids:
441
+ module = self._get_module(cls.id, context)
442
+ if module is None:
443
+ return PythonBaseResolution(
444
+ name=base_name,
445
+ status=ResolutionStatus.UNRESOLVED,
446
+ reason=f"containing module not found for base {base_name}",
341
447
  )
342
- for base_id in sorted(resolved_ids):
343
- if isinstance(self._get_symbol(base_id, context), PythonClass):
344
- base_ids.append(base_id)
345
- return base_ids
448
+ resolved_ids, unresolved_ids = self.resolve_symbol(
449
+ module.fqn, normalized_name, context
450
+ )
346
451
 
347
- def _class_mro_ids(
452
+ class_ids = tuple(
453
+ sorted(
454
+ symbol_id
455
+ for symbol_id in resolved_ids
456
+ if isinstance(self._get_symbol(symbol_id, context), PythonClass)
457
+ )
458
+ )
459
+ if len(class_ids) == 1:
460
+ return PythonBaseResolution(
461
+ name=base_name,
462
+ status=ResolutionStatus.RESOLVED,
463
+ resolved_id=class_ids[0],
464
+ candidates=class_ids,
465
+ )
466
+ if len(class_ids) > 1:
467
+ return PythonBaseResolution(
468
+ name=base_name,
469
+ status=ResolutionStatus.AMBIGUOUS,
470
+ candidates=class_ids,
471
+ reason=f"ambiguous Python base {base_name}",
472
+ )
473
+
474
+ reason = f"Python base {base_name} was not resolved"
475
+ if unresolved_ids:
476
+ reason = f"Python base {base_name} has unresolved import candidates"
477
+ return PythonBaseResolution(
478
+ name=base_name,
479
+ status=ResolutionStatus.UNRESOLVED,
480
+ reason=reason,
481
+ )
482
+
483
+ def _resolve_base_resolutions(
484
+ self,
485
+ cls: PythonClass,
486
+ context: Context,
487
+ base_overrides: Mapping[tuple[SymbolID, str], SymbolID] | None = None,
488
+ ) -> tuple[PythonBaseResolution, ...]:
489
+ return tuple(
490
+ self._resolve_base(cls, base_name, context, base_overrides)
491
+ for base_name in cls.inherits
492
+ if self._normalize_base_name(base_name)
493
+ )
494
+
495
+ def resolve_class_mro(
348
496
  self,
349
497
  class_id: SymbolID,
350
498
  context: Context,
351
- memo: dict[SymbolID, list[SymbolID] | None],
499
+ base_overrides: Mapping[tuple[SymbolID, str], SymbolID] | None = None,
500
+ ) -> PythonClassMROResult:
501
+ return self._resolve_class_mro(
502
+ class_id, context, {}, set(), base_overrides or {}
503
+ )
504
+
505
+ def _resolve_class_mro(
506
+ self,
507
+ class_id: SymbolID,
508
+ context: Context,
509
+ memo: dict[SymbolID, PythonClassMROResult],
352
510
  active: set[SymbolID],
353
- ) -> list[SymbolID] | None:
511
+ base_overrides: Mapping[tuple[SymbolID, str], SymbolID],
512
+ ) -> PythonClassMROResult:
354
513
  if class_id in memo:
355
514
  return memo[class_id]
356
515
  if class_id in active:
357
- return None
516
+ return PythonClassMROResult(
517
+ status=ResolutionStatus.UNRESOLVED,
518
+ mro_ids=(class_id,),
519
+ reason="cyclic Python inheritance",
520
+ )
358
521
 
359
522
  cls = self._get_symbol(class_id, context)
360
523
  if not isinstance(cls, PythonClass):
361
- memo[class_id] = None
362
- return None
524
+ return PythonClassMROResult(
525
+ status=ResolutionStatus.UNRESOLVED,
526
+ reason=f"entity {class_id} is not a Python class",
527
+ )
528
+
363
529
  active.add(class_id)
364
- base_ids = self._resolve_base_ids(cls, context)
365
- if cls.inherits and not base_ids:
366
- active.remove(class_id)
367
- memo[class_id] = None
368
- return None
369
- if not base_ids:
530
+ bases = self._resolve_base_resolutions(cls, context, base_overrides)
531
+ if not bases:
532
+ result = PythonClassMROResult(
533
+ status=ResolutionStatus.RESOLVED,
534
+ mro_ids=(class_id,),
535
+ )
536
+ memo[class_id] = result
370
537
  active.remove(class_id)
371
- memo[class_id] = [class_id]
372
- return memo[class_id]
538
+ return result
373
539
 
374
- base_mros: dict[str, list[str] | None] = {}
375
- base_fqns: list[str] = []
376
- for base_id in base_ids:
377
- base = self._get_symbol(base_id, context)
378
- base_fqn = getattr(base, "fqn", None)
379
- if not base_fqn or base_fqn in base_mros:
380
- continue
381
- base_mro_ids = self._class_mro_ids(base_id, context, memo, active)
382
- if base_mro_ids is None:
383
- active.remove(class_id)
384
- memo[class_id] = None
385
- return None
386
- base_mros[base_fqn] = [
387
- getattr(self._get_symbol(mro_id, context), "fqn", "")
388
- for mro_id in base_mro_ids
389
- ]
390
- base_fqns.append(base_fqn)
540
+ base_mros: list[list[SymbolID] | None] = []
541
+ base_ids: list[SymbolID] = []
542
+ failure_status: ResolutionStatus | None = None
543
+ failure_reason: str | None = None
544
+
545
+ for base in bases:
546
+ if base.status is not ResolutionStatus.RESOLVED or base.resolved_id is None:
547
+ failure_status = base.status
548
+ failure_reason = base.reason
549
+ break
550
+
551
+ base_result = self._resolve_class_mro(
552
+ base.resolved_id,
553
+ context,
554
+ memo,
555
+ active,
556
+ base_overrides,
557
+ )
558
+ if not base_result.mro_ids:
559
+ failure_status = ResolutionStatus.UNRESOLVED
560
+ failure_reason = f"no MRO available for base {base.name}"
561
+ break
562
+ base_mros.append(list(base_result.mro_ids))
563
+ base_ids.append(base.resolved_id)
564
+ if base_result.status is not ResolutionStatus.RESOLVED:
565
+ failure_status = base_result.status
566
+ failure_reason = base_result.reason
567
+ break
568
+
569
+ mro_ids = compute_mro_from_base_ids(class_id, base_mros, base_ids)
570
+ if mro_ids is not None and class_id in mro_ids[1:]:
571
+ mro_ids = [class_id, *[mro_id for mro_id in mro_ids if mro_id != class_id]]
572
+ failure_status = failure_status or ResolutionStatus.UNRESOLVED
573
+ failure_reason = failure_reason or "cyclic Python inheritance"
574
+
575
+ if mro_ids is None:
576
+ result = PythonClassMROResult(
577
+ status=failure_status or ResolutionStatus.UNRESOLVED,
578
+ mro_ids=(class_id,),
579
+ bases=bases,
580
+ reason=failure_reason or f"inconsistent MRO for {cls.fqn}",
581
+ )
582
+ else:
583
+ result = PythonClassMROResult(
584
+ status=failure_status or ResolutionStatus.RESOLVED,
585
+ mro_ids=tuple(mro_ids),
586
+ bases=bases,
587
+ reason=failure_reason,
588
+ )
391
589
 
392
- mro_fqns = compute_mro_from_bases(cls.fqn, base_mros, base_fqns)
393
- if mro_fqns is None:
394
- active.remove(class_id)
395
- memo[class_id] = None
396
- return None
590
+ memo[class_id] = result
591
+ active.remove(class_id)
592
+ return result
593
+
594
+ def get_inherited_members(
595
+ self,
596
+ class_id: SymbolID,
597
+ context: Context,
598
+ mro_result: PythonClassMROResult | None = None,
599
+ ) -> PythonInheritedMembersResult:
600
+ result = mro_result or self.resolve_class_mro(class_id, context)
601
+ if not result.mro_ids:
602
+ return PythonInheritedMembersResult(
603
+ status=result.status,
604
+ reason=result.reason,
605
+ )
397
606
 
398
- mro_ids: list[SymbolID] = []
399
- for mro_fqn in mro_fqns:
400
- if mro_fqn == cls.fqn:
401
- mro_ids.append(class_id)
607
+ cls = self._get_symbol(class_id, context)
608
+ if not isinstance(cls, PythonClass):
609
+ return PythonInheritedMembersResult(
610
+ status=ResolutionStatus.UNRESOLVED,
611
+ mro_ids=result.mro_ids,
612
+ reason=f"entity {class_id} is not a Python class",
613
+ )
614
+
615
+ own_ids = [*cls.method_ids, *cls.property_ids, *cls.inner_type_ids]
616
+ seen_names = {
617
+ getattr(self._get_symbol(member_id, context), "name", "")
618
+ for member_id in own_ids
619
+ }
620
+ members: list[PythonInheritedMember] = []
621
+ for depth, owner_id in enumerate(result.mro_ids[1:], 1):
622
+ owner = self._get_symbol(owner_id, context)
623
+ if not isinstance(owner, PythonClass):
402
624
  continue
403
- candidates = [
404
- candidate_id
405
- for candidate_id in context.entity_registry.get_ids_by_fqn(mro_fqn)
406
- if isinstance(self._get_symbol(candidate_id, context), PythonClass)
625
+ member_ids = [
626
+ *owner.method_ids,
627
+ *owner.property_ids,
628
+ *owner.inner_type_ids,
407
629
  ]
408
- if len(candidates) != 1:
409
- active.remove(class_id)
410
- memo[class_id] = None
411
- return None
412
- mro_ids.append(candidates[0])
630
+ for member_id in member_ids:
631
+ member = self._get_symbol(member_id, context)
632
+ name = getattr(member, "name", "") if member else ""
633
+ if not name or name in seen_names:
634
+ continue
635
+ seen_names.add(name)
636
+ members.append(
637
+ PythonInheritedMember(
638
+ member_id=member_id,
639
+ owner_id=owner_id,
640
+ name=name,
641
+ mro_depth=depth,
642
+ )
643
+ )
413
644
 
414
- active.remove(class_id)
645
+ return PythonInheritedMembersResult(
646
+ status=result.status,
647
+ mro_ids=result.mro_ids,
648
+ members=tuple(members),
649
+ reason=result.reason,
650
+ )
651
+
652
+ def _class_mro_ids(
653
+ self,
654
+ class_id: SymbolID,
655
+ context: Context,
656
+ memo: dict[SymbolID, list[SymbolID] | None],
657
+ active: set[SymbolID],
658
+ ) -> list[SymbolID] | None:
659
+ if class_id in memo:
660
+ return memo[class_id]
661
+ result = self.resolve_class_mro(class_id, context)
662
+ mro_ids = (
663
+ list(result.mro_ids) if result.status is ResolutionStatus.RESOLVED else None
664
+ )
415
665
  memo[class_id] = mro_ids
416
666
  return mro_ids
417
667
 
@@ -421,6 +671,16 @@ class PythonResolver(Resolver[PythonResolverResult]):
421
671
  resolved_ids, unresolved_ids = self.resolve_symbol(
422
672
  module_fqn, symbol_name, context
423
673
  )
674
+ ambiguous_ids = resolved_ids if len(resolved_ids) > 1 else set()
675
+ if ambiguous_ids:
676
+ reason = f"Multiple symbols found for '{module_fqn}.{symbol_name}'"
677
+ elif not resolved_ids:
678
+ reason = f"No symbol found for '{module_fqn}.{symbol_name}'"
679
+ else:
680
+ reason = None
424
681
  return PythonResolverResult(
425
- resolved_ids=resolved_ids, unresolved_ids=unresolved_ids
682
+ resolved_ids=resolved_ids,
683
+ unresolved_ids=unresolved_ids,
684
+ ambiguous_ids=ambiguous_ids,
685
+ reason=reason,
426
686
  )
@@ -1,6 +1,7 @@
1
- from dataclasses import dataclass
1
+ from dataclasses import dataclass, field
2
2
 
3
3
  from deproc.core.interfaces.parser.models import SymbolID
4
+ from deproc.core.interfaces.resolver import ResolutionStatus
4
5
 
5
6
  type ResolvedIDs = set[SymbolID]
6
7
  type UnresolvedIDs = set[SymbolID]
@@ -10,3 +11,55 @@ type UnresolvedIDs = set[SymbolID]
10
11
  class PythonResolverResult:
11
12
  resolved_ids: ResolvedIDs
12
13
  unresolved_ids: UnresolvedIDs
14
+ ambiguous_ids: set[SymbolID] = field(default_factory=set)
15
+ inaccessible_ids: set[SymbolID] = field(default_factory=set)
16
+ reason: str | None = None
17
+
18
+ @property
19
+ def status(self) -> ResolutionStatus:
20
+ if self.ambiguous_ids:
21
+ return ResolutionStatus.AMBIGUOUS
22
+ if self.resolved_ids:
23
+ return ResolutionStatus.RESOLVED
24
+ if self.inaccessible_ids:
25
+ return ResolutionStatus.INACCESSIBLE
26
+ return ResolutionStatus.UNRESOLVED
27
+
28
+ @property
29
+ def candidates(self) -> tuple[SymbolID, ...]:
30
+ return tuple(
31
+ sorted(self.ambiguous_ids or self.resolved_ids or self.inaccessible_ids)
32
+ )
33
+
34
+
35
+ @dataclass(frozen=True)
36
+ class PythonBaseResolution:
37
+ name: str
38
+ status: ResolutionStatus
39
+ resolved_id: SymbolID | None = None
40
+ candidates: tuple[SymbolID, ...] = ()
41
+ reason: str | None = None
42
+
43
+
44
+ @dataclass(frozen=True)
45
+ class PythonClassMROResult:
46
+ status: ResolutionStatus
47
+ mro_ids: tuple[SymbolID, ...] = ()
48
+ bases: tuple[PythonBaseResolution, ...] = ()
49
+ reason: str | None = None
50
+
51
+
52
+ @dataclass(frozen=True)
53
+ class PythonInheritedMember:
54
+ member_id: SymbolID
55
+ owner_id: SymbolID
56
+ name: str
57
+ mro_depth: int
58
+
59
+
60
+ @dataclass(frozen=True)
61
+ class PythonInheritedMembersResult:
62
+ status: ResolutionStatus
63
+ mro_ids: tuple[SymbolID, ...] = ()
64
+ members: tuple[PythonInheritedMember, ...] = ()
65
+ reason: str | None = None
@@ -85,13 +85,15 @@ def _compute_exports(
85
85
  modules = groups.get(module_fqn, [])
86
86
  names: set[str] = set()
87
87
  explicit = False
88
- dynamic = False
88
+ explicit_names: set[str] = set()
89
+ own_dynamic = False
90
+ dependency_dynamic = False
89
91
  for module in modules:
90
92
  if getattr(module, "exports_dynamic", False):
91
- dynamic = True
93
+ own_dynamic = True
92
94
  if module.all_exports is not None:
93
95
  explicit = True
94
- names.update(module.all_exports)
96
+ explicit_names.update(module.all_exports)
95
97
  else:
96
98
  names.update(_direct_exports(module, registry))
97
99
 
@@ -103,14 +105,21 @@ def _compute_exports(
103
105
  continue
104
106
  target_fqn = _module_target(import_statement, module, registry)
105
107
  if not target_fqn:
106
- dynamic = True
108
+ dependency_dynamic = True
107
109
  continue
108
110
  if target_fqn in groups:
109
111
  names.update(visit(target_fqn))
110
- dynamic = dynamic or target_fqn in dynamic_modules
112
+ dependency_dynamic = (
113
+ dependency_dynamic or target_fqn in dynamic_modules
114
+ )
111
115
  else:
112
- dynamic = True
116
+ dependency_dynamic = True
113
117
 
118
+ if explicit:
119
+ names = explicit_names
120
+ dynamic = own_dynamic
121
+ else:
122
+ dynamic = own_dynamic or dependency_dynamic
114
123
  if not explicit and not modules:
115
124
  dynamic = True
116
125
  if dynamic:
@@ -33,3 +33,21 @@ def compute_mro_from_bases(
33
33
  return [self_fqn, *c3_merge(merge_lists)]
34
34
  except ValueError:
35
35
  return None
36
+
37
+
38
+ def compute_mro_from_base_ids(
39
+ self_id: str,
40
+ base_mros: list[list[str] | None],
41
+ base_ids: list[str],
42
+ ) -> list[str] | None:
43
+ """Compute a C3 MRO while preserving entity identity."""
44
+ base_mro_lists: list[list[str]] = []
45
+ for mro in base_mros:
46
+ if mro is None:
47
+ return None
48
+ base_mro_lists.append(list(mro))
49
+
50
+ try:
51
+ return [self_id, *c3_merge([*base_mro_lists, list(base_ids)])]
52
+ except ValueError:
53
+ return None
@@ -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:
@@ -278,6 +281,7 @@ def record_to_entity(record: dict) -> Entity | None:
278
281
  end_lineno=meta.get("end_lineno", 0),
279
282
  col_offset=meta.get("col_offset", 0),
280
283
  end_col_offset=meta.get("end_col_offset", 0),
284
+ source_id=meta.get("source_id"),
281
285
  )
282
286
  common = {
283
287
  "id": record["id"],
@@ -315,6 +319,7 @@ def record_to_entity(record: dict) -> Entity | None:
315
319
  )
316
320
  return PythonFunctionLike(
317
321
  name=record["name"],
322
+ parent_id=parent_id,
318
323
  source_range=sr,
319
324
  docstring_range=None,
320
325
  signature=signature,
@@ -326,17 +331,23 @@ def record_to_entity(record: dict) -> Entity | None:
326
331
  if entity_class is PythonClass:
327
332
  return PythonClass(
328
333
  name=record["name"],
334
+ parent_id=parent_id,
329
335
  source_range=sr,
330
336
  docstring_range=None,
337
+ method_ids=meta.get("method_ids", []),
338
+ inner_type_ids=meta.get("inner_type_ids", []),
339
+ property_ids=meta.get("property_ids", []),
331
340
  visibility=meta.get("visibility"),
332
341
  inherits=meta.get("parent_classes", []),
333
342
  **common,
334
343
  )
335
344
  if entity_class is PythonModule:
336
345
  return PythonModule(
346
+ parent_id=parent_id,
337
347
  all_exports=meta.get("all_exports"),
338
348
  exports_dynamic=meta.get("exports_dynamic", False),
339
349
  path=meta.get("path", ""),
350
+ source_root_id=meta.get("source_root_id"),
340
351
  source="",
341
352
  docstring_range=None,
342
353
  import_stmt_ids=meta.get("import_stmt_ids", []),
@@ -348,10 +359,12 @@ def record_to_entity(record: dict) -> Entity | None:
348
359
  )
349
360
  if entity_class is PythonPackage:
350
361
  return PythonPackage(
362
+ parent_id=parent_id,
351
363
  submodule_ids=meta.get("submodule_ids", []),
352
364
  all_exports=meta.get("all_exports"),
353
365
  exports_dynamic=meta.get("exports_dynamic", False),
354
366
  path=meta.get("path", ""),
367
+ source_root_id=meta.get("source_root_id"),
355
368
  source="",
356
369
  docstring_range=None,
357
370
  import_stmt_ids=meta.get("import_stmt_ids", []),
@@ -363,6 +376,7 @@ def record_to_entity(record: dict) -> Entity | None:
363
376
  )
364
377
  if entity_class is PythonNamespacePackage:
365
378
  return PythonNamespacePackage(
379
+ parent_id=parent_id,
366
380
  path=meta.get("path", ""),
367
381
  submodule_ids=meta.get("submodule_ids", []),
368
382
  **common,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deproc-python
3
- Version: 0.2.0
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
@@ -0,0 +1,25 @@
1
+ deproc/plugins/python/__init__.py,sha256=JuMffHnYtF82voXTOP4m1hQ-zTGA5OeroWhHuz9v8nk,504
2
+ deproc/plugins/python/linker/__init__.py,sha256=gQ64CQcNEDHfvyAuYAz81PZ6r2V0ZdnsVOR2YZZvvfU,59
3
+ deproc/plugins/python/linker/main.py,sha256=As8K2k1PtxdO7GnFC4cRQXRDzVJOFWqKPLx5O_xj2V4,3641
4
+ deproc/plugins/python/linker/models.py,sha256=aXigj6ATiTmM_wQVjSQP_Te8An6wm_XhR3InauqRaMc,1198
5
+ deproc/plugins/python/parser/__init__.py,sha256=dcfU6uyIOBUOvnz9xM4hX6gc0ldTGavHZCSIv6lbWWk,71
6
+ deproc/plugins/python/parser/main.py,sha256=hXODQpsCTOnTmi4xlTbGhl95SyusU5RmYMFZm5obtCI,31197
7
+ deproc/plugins/python/parser/models.py,sha256=_Fr239_t-v6ET5EA1uUVTUiFHztqbJ03Q7WfdktrQnc,1700
8
+ deproc/plugins/python/parser/utils/extraction.py,sha256=UE5u6SA_KrrGX6hTbuW1RrlFRvNqrlA-jMpGepHX0z0,7221
9
+ deproc/plugins/python/parser/utils/misc.py,sha256=vdxTcrNdWndIvaZtC8zGlKYI6XZnZHZ7I7YskuvXdnQ,219
10
+ deproc/plugins/python/parser/utils/tree_sitter_python.py,sha256=cBkzIlVjT1N7E7Vux9h5ixDF66M8UIn6ueo5OBz6yj0,814
11
+ deproc/plugins/python/resolver/__init__.py,sha256=SBOVVzKlc-DxeXsrM3QYSm2qBjefA8h3Pm7wlhG0QwY,328
12
+ deproc/plugins/python/resolver/main.py,sha256=hPmX84x86EcOhAEdT1RbsgADv_jdGqsRANDBQjM7b24,25534
13
+ deproc/plugins/python/resolver/models.py,sha256=Q5BkCtjIoucj3VZw_YA1VN84y3aTDVLPT0RtlU64j-U,1766
14
+ deproc/plugins/python/symbol_cache/__init__.py,sha256=3Ob_B64ZY-_uA8QHkYJ3TBSD_S6wRW2E0cng42lqyrw,69
15
+ deproc/plugins/python/symbol_cache/main.py,sha256=lVoiscGkTGIN_ui-3DdKcKTys54vjeCtBdahEOGpLWA,2831
16
+ deproc/plugins/python/symbol_cache/models.py,sha256=1SgqUyhNnwZcITRCL8a-21T26niQ4DNtuaXs7bftXAc,284
17
+ deproc/plugins/python/utils/exports.py,sha256=45dvBfClVEo_ZRN6vCPEx0VH3-eRQiKahv1Qgg074Lo,5218
18
+ deproc/plugins/python/utils/imports.py,sha256=DWvcN2SdYmhQgRl9RYAc5nxbbijSEtAJ11X_Xrf3x_U,547
19
+ deproc/plugins/python/utils/mro.py,sha256=6tTC5lHFK2PZWX-454dGKNmiVhFDrMRQhI0bgDJZlrk,1565
20
+ deproc/plugins/python/utils/packages.py,sha256=d6gUP1_KE8_r3StPpdRpH5TyWJnaMlpXVqzBOFEm1HI,3861
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,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (84.0.0)
2
+ Generator: setuptools (84.2.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,24 +0,0 @@
1
- deproc/plugins/python/__init__.py,sha256=U6pyicyWuoAM55A6DDtiw0QApFdajcW_v-rpHzFcKzc,261
2
- deproc/plugins/python/linker/__init__.py,sha256=gQ64CQcNEDHfvyAuYAz81PZ6r2V0ZdnsVOR2YZZvvfU,59
3
- deproc/plugins/python/linker/main.py,sha256=rulxUoRRu4l7RbJKm2oBAxowHYv5KpsKvV36XRmvrjw,3260
4
- deproc/plugins/python/linker/models.py,sha256=Q5_SFhaGyU8-HSNnIj0t00SYXqdnfwChyO6d5OcUNXM,492
5
- deproc/plugins/python/parser/__init__.py,sha256=dcfU6uyIOBUOvnz9xM4hX6gc0ldTGavHZCSIv6lbWWk,71
6
- deproc/plugins/python/parser/main.py,sha256=UYJwEODEaHZOZaxqLY_0nmuO3lDsqxnks6-AhnorZvg,31146
7
- deproc/plugins/python/parser/models.py,sha256=_Fr239_t-v6ET5EA1uUVTUiFHztqbJ03Q7WfdktrQnc,1700
8
- deproc/plugins/python/parser/utils/extraction.py,sha256=UE5u6SA_KrrGX6hTbuW1RrlFRvNqrlA-jMpGepHX0z0,7221
9
- deproc/plugins/python/parser/utils/misc.py,sha256=vdxTcrNdWndIvaZtC8zGlKYI6XZnZHZ7I7YskuvXdnQ,219
10
- deproc/plugins/python/parser/utils/tree_sitter_python.py,sha256=cBkzIlVjT1N7E7Vux9h5ixDF66M8UIn6ueo5OBz6yj0,814
11
- deproc/plugins/python/resolver/__init__.py,sha256=UfP8yJQhc-rYsHh7lRuL4CvyEeoLxc6VQk3pGYETGFs,63
12
- deproc/plugins/python/resolver/main.py,sha256=6QSNVXLt19Y0V7tuNZXMG6UxxP75NCgHcHFaFDD7SlY,15525
13
- deproc/plugins/python/resolver/models.py,sha256=hwO_HqzIF5CFVTeXYx5UX2dT92uI7TLMmuHOlEqGJsA,267
14
- deproc/plugins/python/symbol_cache/__init__.py,sha256=3Ob_B64ZY-_uA8QHkYJ3TBSD_S6wRW2E0cng42lqyrw,69
15
- deproc/plugins/python/symbol_cache/main.py,sha256=lVoiscGkTGIN_ui-3DdKcKTys54vjeCtBdahEOGpLWA,2831
16
- deproc/plugins/python/symbol_cache/models.py,sha256=1SgqUyhNnwZcITRCL8a-21T26niQ4DNtuaXs7bftXAc,284
17
- deproc/plugins/python/utils/exports.py,sha256=o2Ub4j1HPkEmrlAvF3niIMglaixbasEPIcca_-by1xM,4873
18
- deproc/plugins/python/utils/imports.py,sha256=DWvcN2SdYmhQgRl9RYAc5nxbbijSEtAJ11X_Xrf3x_U,547
19
- deproc/plugins/python/utils/mro.py,sha256=sPDG__W9FYkTsrgfaFXs9IV6R_Kzf9a3uPrpGjFE7h0,1086
20
- deproc/plugins/python/utils/serialization.py,sha256=heYGQMcUxCxtBHmR7KF3twECM3ddsILUuP72_k_EKA8,16286
21
- deproc_python-0.2.0.dist-info/METADATA,sha256=ho9yeeOJQ8q79SziDvnmjZltS6Dy7vkHFIZqByAbPNU,949
22
- deproc_python-0.2.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
23
- deproc_python-0.2.0.dist-info/top_level.txt,sha256=qyPIAnJVTLaewuAbLpShmn-777EtCI7b1b5sGgkzO10,7
24
- deproc_python-0.2.0.dist-info/RECORD,,