The directory a ``from import ...`` names when that module is a PEP 420 namespace package: a directory under the scan root with no ``__init__.py``. ``_resolve_python_module_path`` returns None for it (there is no module file to probe), so a package that omits ``__init__.py`` --
(module_name: str, current_path: Path, root: Path, level: int)
| 3723 | return None |
| 3724 | |
| 3725 | def _resolve_python_namespace_dir(module_name: str, current_path: Path, root: Path, level: int) -> "Path | None": |
| 3726 | """The directory a ``from <module> import ...`` names when that module is a |
| 3727 | PEP 420 namespace package: a directory under the scan root with no |
| 3728 | ``__init__.py``. ``_resolve_python_module_path`` returns None for it (there is |
| 3729 | no module file to probe), so a package that omits ``__init__.py`` -- which |
| 3730 | ``python -m pkg.mod`` runs without complaint -- had every ``from . import |
| 3731 | sibling`` dropped whole, and with it every ``sibling.func()`` call the #1883 |
| 3732 | module arm would otherwise have resolved: the most-called functions in such a |
| 3733 | repo carried in-degree 0. Mirrors that resolver's walk (relative base, then |
| 3734 | scan root, then sys.path-root ancestors) and returns only a directory that |
| 3735 | exists inside the root.""" |
| 3736 | def _namespace(candidate: Path) -> "Path | None": |
| 3737 | if not candidate.is_dir() or _is_python_package_dir(candidate): |
| 3738 | return None |
| 3739 | try: |
| 3740 | _resolve_cached(candidate).relative_to(_resolve_cached(root)) |
| 3741 | except ValueError: |
| 3742 | return None |
| 3743 | return candidate |
| 3744 | |
| 3745 | if level > 0: |
| 3746 | base = current_path.parent |
| 3747 | for _ in range(level - 1): |
| 3748 | base = base.parent |
| 3749 | return _namespace(base / module_name.replace(".", "/") if module_name else base) |
| 3750 | if not module_name: |
| 3751 | return None |
| 3752 | rel = module_name.replace(".", "/") |
| 3753 | hit = _namespace(root / rel) |
| 3754 | if hit is not None: |
| 3755 | return hit |
| 3756 | |
| 3757 | # NEW: Scan-root namespace projection (#3843). |
| 3758 | ns = _infer_scan_root_namespace(root) |
| 3759 | if ns and (module_name == ns or module_name.startswith(ns + ".")): |
| 3760 | stripped = module_name[len(ns) + 1:] if module_name != ns else "" |
| 3761 | if stripped: |
| 3762 | hit = _namespace(root / stripped.replace(".", "/")) |
| 3763 | if hit is not None: |
| 3764 | return hit |
| 3765 | else: |
| 3766 | hit = _namespace(root) |
| 3767 | if hit is not None: |
| 3768 | return hit |
| 3769 | for anc in current_path.parents: |
| 3770 | try: |
| 3771 | anc.relative_to(root) |
| 3772 | except ValueError: |
| 3773 | break # left the scan root; stop walking up |
| 3774 | if anc == root or _is_python_package_dir(anc): |
| 3775 | continue # root already probed; a package dir is not a sys.path root (#2072) |
| 3776 | hit = _namespace(anc / rel) |
| 3777 | if hit is not None: |
| 3778 | return hit |
| 3779 | return None |
| 3780 | |
| 3781 | def _python_top_level_function_bodies(path: Path, root_node, source: bytes) -> list[tuple[str, object]]: |
| 3782 | """``(plain caller id, body)`` for every top-level ``def`` and every method of |