MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / resolvePythonModule

Function resolvePythonModule

src/resolution/import-resolver.ts:891–966  ·  view source on GitHub ↗

* Resolve a Python import specifier to the file of the module it names — * `pkg.sub.mod` → `pkg/sub/mod.py`, `pkg` → `pkg/__init__.py`, `..sibling` * relative to `fromFile`'s package. Null when the module isn't in the index, * when a relative import climbs above its top-level package, or when two

(
  specifier: string,
  fromFile: string,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

889 * project roots that don't contain the importer both provide it.
890 */
891function resolvePythonModule(
892 specifier: string,
893 fromFile: string,
894 context: ResolutionContext
895): string | null {
896 if (!/^\.*[\w.]*$/.test(specifier) || specifier === '') return null;
897 const index = pythonModuleIndex(context);
898 const importerDir = parentDir(fromFile.replace(/\\/g, '/'));
899
900 // Relative: leading dots are package levels (one dot = the importer's own
901 // package), the rest a dotted path below it.
902 const dots = specifier.length - specifier.replace(/^\.+/, '').length;
903 if (dots > 0) {
904 let base = importerDir;
905 for (let level = 1; level < dots; level++) {
906 // Python refuses to climb out of the top-level package ("attempted
907 // relative import beyond top-level package"). Only a regular package
908 // proves where the top is; a namespace package's boundary is unknown.
909 if (index.packageDirs.has(base) && !index.packageDirs.has(parentDir(base))) return null;
910 if (!base) return null;
911 base = parentDir(base);
912 }
913 const rest = specifier.slice(dots);
914 if (rest) return pythonModuleUnder(base, rest.replace(/\./g, '/'), index);
915 // `from . import x` — the package itself, i.e. its `__init__.py`.
916 const init = joinRel(base, '__init__.py');
917 return index.files.has(init) ? init : null;
918 }
919
920 const relPath = specifier.replace(/\./g, '/');
921 const head = specifier.split('.')[0]!;
922
923 // Roots that contain the importer, nearest first — the ones Python would
924 // search for this file before any other.
925 const containing: string[] = [];
926 for (let dir: string | null = importerDir; dir !== null; dir = dir ? parentDir(dir) : null) {
927 const isRoot =
928 index.projectRoots.has(dir) ||
929 index.localRoots.has(dir) ||
930 (!index.packageDirs.has(dir) &&
931 (dir === importerDir || importerDir === joinRel(dir, head) || importerDir.startsWith(`${joinRel(dir, head)}/`)));
932 if (isRoot) containing.push(dir);
933 }
934
935 // A root containing the importer is on its path: the nearest one that holds
936 // the module answers. If one holds `head` itself as a regular package or
937 // module but not the rest of the path, Python stops there too.
938 let headIsLocal = false;
939 for (const root of containing) {
940 const hit = pythonModuleUnder(root, relPath, index);
941 if (hit) return hit;
942 if (pythonHeadKind(root, head, index) === 'concrete') headIsLocal = true;
943 }
944 if (headIsLocal) return null;
945
946 // Otherwise a root elsewhere whose reach includes the importer, as
947 // `sys.path` would rank them: a root holding `head` concretely shadows any
948 // namespace portion, and either way the answer must be unambiguous.

Callers 1

Calls 10

pythonModuleIndexFunction · 0.85
pythonModuleUnderFunction · 0.85
joinRelFunction · 0.85
pythonHeadKindFunction · 0.85
pythonRootHeadsFunction · 0.85
pythonModuleOfScriptFunction · 0.85
hasMethod · 0.80
pushMethod · 0.80
parentDirFunction · 0.70
reachesFunction · 0.70

Tested by

no test coverage detected