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

Function resolvePythonModule

src/resolution/import-resolver.ts:889–964  ·  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

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