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

Function _repoint_python_package_imports

graphify/extract.py:533–615  ·  view source on GitHub ↗

Repoint Python absolute-import edges to the real file node under a nested (e.g. ``src/``) package root (#2072). Absolute imports target an id derived from the dotted module path (``_make_id('pkg.mod')`` -> ``pkg_mod``), but file-node ids are scan-root-relative (``src_pkg_mod`` when

(paths, all_nodes, all_edges, root)

Source from the content-addressed store, hash-verified

531
532
533def _repoint_python_package_imports(paths, all_nodes, all_edges, root) -> None:
534 """Repoint Python absolute-import edges to the real file node under a nested
535 (e.g. ``src/``) package root (#2072).
536
537 Absolute imports target an id derived from the dotted module path
538 (``_make_id('pkg.mod')`` -> ``pkg_mod``), but file-node ids are
539 scan-root-relative (``src_pkg_mod`` when the code lives under ``src/``), so
540 the edge dangles and is silently dropped — the graph loses most ``imports``
541 edges purely because of where the scan started. Build an alias map from the
542 dotted-module id to the real file-node id by detecting each ``.py`` file's
543 package root (the contiguous run of ancestor dirs carrying a Python initializer)
544 and rewrite matching ``imports``/``imports_from`` edge targets. Guards: never
545 shadow an existing node id, and drop an alias claimed by more than one file
546 (ambiguous -> leave dangling, as before). Files whose package root IS the
547 scan root are skipped (ids already coincide)."""
548 try:
549 root = Path(root).resolve()
550 except OSError:
551 root = Path(root)
552 node_ids = {n.get("id") for n in all_nodes if isinstance(n, dict)}
553 alias_to_files: dict[str, set[str]] = {}
554 for p in paths:
555 if effective_suffix(p).lower() not in (".py", ".pyi"):
556 continue
557 try:
558 rel = _resolve_cached(p).relative_to(root)
559 except (ValueError, OSError):
560 continue
561 parts = rel.parts
562 if len(parts) < 2:
563 continue # top-level file: scan-root-relative id already matches
564 d = _resolve_cached(p).parent
565 levels = 0
566 # Bounded by the number of dirs between the file and the scan root, so a
567 # pathological `/__init__.py` chain can't loop forever.
568 while levels < len(parts) - 1 and _is_python_package_dir(d):
569 levels += 1
570 d = d.parent
571 if levels == 0:
572 # No initializer above the file: a PEP 420 namespace package, or a
573 # loose module. Under a `src/` layout the import root is that `src/`,
574 # so `pkgsql/src/airflow/sql/hooks/sql.py` is `airflow.sql.hooks.sql`
575 # even though no directory carries an `__init__.py`. Without this a
576 # monorepo of namespace distributions dropped every cross-distribution
577 # import, and with it inherited `self.method()` calls.
578 mod_parts = _namespace_src_module_parts(parts)
579 if mod_parts is None:
580 continue # loose module, not under a src/ root
581 else:
582 mod_parts = parts[-(levels + 1):] # package dirs + the file itself
583 if len(mod_parts) == len(parts):
584 continue # package root == scan root: file-node id already coincides
585 file_node = _file_node_id(rel)
586 alias = _make_id(str(Path(*mod_parts).with_suffix("")))
587 alias_to_files.setdefault(alias, set()).add(file_node)
588 if (
589 p.stem == "__init__"
590 and effective_suffix(p) in (".py", ".pyi")

Callers 1

extractFunction · 0.85

Calls 8

effective_suffixFunction · 0.90
_is_python_package_dirFunction · 0.90
_make_idFunction · 0.90
_file_node_idFunction · 0.70
resolveMethod · 0.45
getMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected