MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _resolve_python_module_path

Function _resolve_python_module_path

graphify/extractors/resolution.py:3652–3723  ·  view source on GitHub ↗
(module_name: str, current_path: Path, root: Path, level: int)

Source from the content-addressed store, hash-verified

3650
3651
3652def _resolve_python_module_path(module_name: str, current_path: Path, root: Path, level: int) -> Path | None:
3653 if level > 0:
3654 base = current_path.parent
3655 for _ in range(level - 1):
3656 base = base.parent
3657 candidate = base / module_name.replace(".", "/") if module_name else base
3658 return _probe_python_module_candidate(candidate)
3659
3660 # Absolute import. Probe the scan root first (unchanged for the common
3661 # root-is-package-root layout), then walk up from the importing file toward
3662 # the root so a `src/` (or otherwise nested) package root resolves regardless
3663 # of where the scan started — `import pkg.mod` from src/pkg/app.py must find
3664 # src/pkg/mod.py whether the scan root is the repo or src/ (#2072). Mirrors
3665 # the upward walk already used for Lua (_resolve_lua_import_target, #1075).
3666 rel = module_name.replace(".", "/")
3667 # A bare absolute import whose top-level name is a stdlib module means the
3668 # stdlib module, not a loose same-named local file (e.g. a non-package
3669 # `scripts/logging.py` must not capture `import logging`, #4261). A deliberate
3670 # package shadow (`logging/__init__.py`) is a stronger signal of intent and is
3671 # still resolved; only loose .py modules and namespace dirs are refused.
3672 stdlib_shadow = module_name.split(".", 1)[0] in _PYTHON_STDLIB_MODULE_NAMES
3673
3674 def _accept(candidate: Path | None) -> Path | None:
3675 if (
3676 candidate is not None
3677 and stdlib_shadow
3678 and candidate.name not in ("__init__.py", "__init__.pyi")
3679 ):
3680 return None
3681 return candidate
3682
3683 hit = _accept(_probe_python_module_candidate(root / rel))
3684 if hit is not None:
3685 return hit
3686
3687 # NEW: Scan-root namespace projection (#3843).
3688 # When the scan root is nested inside a package hierarchy
3689 # (e.g., root = Team/, namespace = Company.Apps.Jobs.Team),
3690 # an import like "Company.Apps.Jobs.Team.lib" fails the probe above
3691 # because root/Company/Apps/Jobs/Team/lib doesn't exist. Strip the
3692 # namespace prefix and re-probe relative to root.
3693 ns = _infer_scan_root_namespace(root)
3694 if ns and (module_name == ns or module_name.startswith(ns + ".")):
3695 stripped = module_name[len(ns) + 1:] if module_name != ns else ""
3696 if stripped:
3697 hit = _accept(_probe_python_module_candidate(root / stripped.replace(".", "/")))
3698 if hit is not None:
3699 return hit
3700 else:
3701 hit = _accept(_probe_python_module_candidate(root))
3702 if hit is not None:
3703 return hit
3704
3705 for anc in current_path.parents:
3706 try:
3707 anc.relative_to(root)
3708 except ValueError:
3709 break # left the scan root; stop walking up

Calls 4

_acceptFunction · 0.85
_is_python_package_dirFunction · 0.85