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

Function resolvePythonModule

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

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

Tested by

no test coverage detected