Map importable absolute module ids to their scanned file paths. A file can be importable from more than one sys.path root inside the scan root (for example ``a/src/pkg/mod.py`` as either ``pkg.mod`` or ``src.pkg.mod``). Probe each in-root, non-package ancestor the same way the share
(paths, root)
| 284 | |
| 285 | |
| 286 | def _python_absolute_import_alias_files(paths, root) -> tuple[dict[str, set[str]], set[str]]: |
| 287 | """Map importable absolute module ids to their scanned file paths. |
| 288 | |
| 289 | A file can be importable from more than one sys.path root inside the scan |
| 290 | root (for example ``a/src/pkg/mod.py`` as either ``pkg.mod`` or |
| 291 | ``src.pkg.mod``). Probe each in-root, non-package ancestor the same way the |
| 292 | shared resolver does. Keeping every candidate lets the extraction passes |
| 293 | fail closed when the same absolute module name identifies multiple files. |
| 294 | """ |
| 295 | try: |
| 296 | root = Path(root).resolve() |
| 297 | except OSError: |
| 298 | root = Path(root) |
| 299 | |
| 300 | package_dirs: dict[Path, bool] = {} |
| 301 | |
| 302 | def _is_package(path: Path) -> bool: |
| 303 | if path not in package_dirs: |
| 304 | package_dirs[path] = (path / "__init__.py").is_file() |
| 305 | return package_dirs[path] |
| 306 | |
| 307 | aliases: dict[str, set[str]] = {} |
| 308 | scan_root_aliases: set[str] = set() |
| 309 | for raw_path in paths: |
| 310 | p = Path(raw_path) |
| 311 | if p.suffix.lower() != ".py": |
| 312 | # _resolve_python_module_path probes .py, not a lone .pyi file. |
| 313 | continue |
| 314 | try: |
| 315 | p = _resolve_cached(p) |
| 316 | rel = p.relative_to(root) |
| 317 | except (ValueError, OSError, RuntimeError): |
| 318 | continue |
| 319 | |
| 320 | module_path = p.parent if p.name == "__init__.py" else p.with_suffix("") |
| 321 | candidate_roots = [root] |
| 322 | for ancestor in p.parents: |
| 323 | if ancestor == root: |
| 324 | break |
| 325 | try: |
| 326 | ancestor.relative_to(root) |
| 327 | except ValueError: |
| 328 | break |
| 329 | if not _is_package(ancestor): |
| 330 | candidate_roots.append(ancestor) |
| 331 | |
| 332 | for candidate_root in candidate_roots: |
| 333 | try: |
| 334 | module_rel = module_path.relative_to(candidate_root) |
| 335 | except ValueError: |
| 336 | continue |
| 337 | if not module_rel.parts or not all(part.isidentifier() for part in module_rel.parts): |
| 338 | continue |
| 339 | module_name = ".".join(module_rel.parts) |
| 340 | candidate = candidate_root.joinpath(*module_rel.parts) |
| 341 | try: |
| 342 | hit = _probe_python_module_candidate(candidate) |
| 343 | if hit is None or _resolve_cached(hit) != p: |
no test coverage detected