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

Function resolvePythonModule

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

1135 * project roots that don't contain the importer both provide it.
1136 */
1137function resolvePythonModule(
1138 specifier: string,
1139 fromFile: string,
1140 context: ResolutionContext
1141): string | null {
1142 if (!/^\.*[\w.]*$/.test(specifier) || specifier === '') return null;
1143 const index = pythonModuleIndex(context);
1144 const importerDir = parentDir(fromFile.replace(/\\/g, '/'));
1145
1146 // Relative: leading dots are package levels (one dot = the importer's own
1147 // package), the rest a dotted path below it.
1148 const dots = specifier.length - specifier.replace(/^\.+/, '').length;
1149 if (dots > 0) {
1150 let base = importerDir;
1151 for (let level = 1; level < dots; level++) {
1152 // Python refuses to climb out of the top-level package ("attempted
1153 // relative import beyond top-level package"). Only a regular package
1154 // proves where the top is; a namespace package's boundary is unknown.
1155 if (index.packageDirs.has(base) && !index.packageDirs.has(parentDir(base))) return null;
1156 if (!base) return null;
1157 base = parentDir(base);
1158 }
1159 const rest = specifier.slice(dots);
1160 if (rest) return pythonModuleUnder(base, rest.replace(/\./g, '/'), index);
1161 // `from . import x` — the package itself, i.e. its `__init__.py`.
1162 const init = joinRel(base, '__init__.py');
1163 return index.files.has(init) ? init : null;
1164 }
1165
1166 const relPath = specifier.replace(/\./g, '/');
1167 const head = specifier.split('.')[0]!;
1168
1169 // Roots that contain the importer, nearest first — the ones Python would
1170 // search for this file before any other.
1171 const containing: string[] = [];
1172 for (let dir: string | null = importerDir; dir !== null; dir = dir ? parentDir(dir) : null) {
1173 const isRoot =
1174 index.projectRoots.has(dir) ||
1175 index.localRoots.has(dir) ||
1176 (!index.packageDirs.has(dir) &&
1177 (dir === importerDir || importerDir === joinRel(dir, head) || importerDir.startsWith(`${joinRel(dir, head)}/`)));
1178 if (isRoot) containing.push(dir);
1179 }
1180
1181 // A root containing the importer is on its path: the nearest one that holds
1182 // the module answers. If one holds `head` itself as a regular package or
1183 // module but not the rest of the path, Python stops there too.
1184 let headIsLocal = false;
1185 for (const root of containing) {
1186 const hit = pythonModuleUnder(root, relPath, index);
1187 if (hit) return hit;
1188 if (pythonHeadKind(root, head, index) === 'concrete') headIsLocal = true;
1189 }
1190 if (headIsLocal) return null;
1191
1192 // Otherwise a root elsewhere whose reach includes the importer, as
1193 // `sys.path` would rank them: a root holding `head` concretely shadows any
1194 // 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