(
node, source: bytes, file_nid: str, stem: str, edges: list, str_path: str,
scope_stack: list[str] | None = None, scan_root: Path | None = None,
)
| 972 | |
| 973 | |
| 974 | def _import_python( |
| 975 | node, source: bytes, file_nid: str, stem: str, edges: list, str_path: str, |
| 976 | scope_stack: list[str] | None = None, scan_root: Path | None = None, |
| 977 | ) -> None: |
| 978 | t = node.type |
| 979 | # _resolve_cached, not Path.resolve(): both of these are per-FILE constants |
| 980 | # asked for once per import statement, and on Windows each one is an |
| 981 | # nt._getfinalpathname walk -- 7,413 resolves, 8.5 s of a 384.5 s cold |
| 982 | # in-process extraction of graphify's own source. Same per-run memo (keyed |
| 983 | # by path plus cwd, cleared at the top of extract()) the symbol-resolution |
| 984 | # passes use, and it raises exactly like Path.resolve(). |
| 985 | current_path = Path(str_path) |
| 986 | try: |
| 987 | current_path = _resolve_cached(current_path) |
| 988 | except OSError: |
| 989 | pass |
| 990 | root = Path(scan_root) if scan_root is not None else current_path.parent |
| 991 | try: |
| 992 | root = _resolve_cached(root) |
| 993 | except OSError: |
| 994 | pass |
| 995 | # Imports under `if TYPE_CHECKING:` never run, so they are stamped `type_only` |
| 996 | # like TS `import type`: find_import_cycles skips them, the edge stays (#3159). |
| 997 | type_only = _python_under_type_checking(node, source) |
| 998 | if t == "import_statement": |
| 999 | for child in node.children: |
| 1000 | if child.type in ("dotted_name", "aliased_import"): |
| 1001 | raw = _read_text(child, source) |
| 1002 | raw_module, _, raw_alias = raw.partition(" as ") |
| 1003 | module_name = raw_module.strip().lstrip(".") |
| 1004 | local_binding = raw_alias.strip() if raw_alias else module_name.split(".")[0] |
| 1005 | target_path = _resolve_python_module_path( |
| 1006 | module_name, current_path, root, level=0 |
| 1007 | ) |
| 1008 | # Keep the target-file stamp even for a nested sys.path root so |
| 1009 | # incremental extraction can canonicalize imports whose target |
| 1010 | # is not in this batch. The corpus-wide ambiguity guard runs |
| 1011 | # before symbol resolution and clears this edge when another |
| 1012 | # scanned file claims the same absolute module name. |
| 1013 | if target_path is not None: |
| 1014 | try: |
| 1015 | if target_path.resolve() == current_path: |
| 1016 | target_path = None |
| 1017 | except OSError: |
| 1018 | pass |
| 1019 | tgt_nid = ( |
| 1020 | _make_id(str(target_path)) |
| 1021 | if target_path is not None |
| 1022 | else _make_id(module_name) |
| 1023 | ) |
| 1024 | edge = { |
| 1025 | "source": file_nid, |
| 1026 | "target": tgt_nid, |
| 1027 | "relation": "imports", |
| 1028 | "context": "import", |
| 1029 | "confidence": "EXTRACTED", |
| 1030 | "source_file": str_path, |
| 1031 | "source_location": f"L{node.start_point[0] + 1}", |
nothing calls this directly
no test coverage detected