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

Function pythonModuleOfScript

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

* The module only a script's own `sys.path[0]` provides, which no root does: * `from test import TestCase` in `modules/vs/tests/with_a.py`, with the runner * `modules/vs/test.py` above it, or `from pysim import HashTable` in * `tools/an/pysim_test.py`, beside `tools/an/pysim.py` in a folder that

(
  specifier: string,
  relPath: string,
  fromFile: string,
  importerDir: string,
  index: PythonModuleIndex,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

963 * it is no root, so it is passed over.
964 */
965function pythonModuleOfScript(
966 specifier: string,
967 relPath: string,
968 fromFile: string,
969 importerDir: string,
970 index: PythonModuleIndex,
971 context: ResolutionContext
972): string | null {
973 if (PYTHON_STDLIB_MODULES.has(specifier.split('.')[0]!)) return null;
974 const taken = context.getImportMappings(fromFile, 'python')
975 .filter((imp) => imp.source === specifier && !imp.isNamespace)
976 .map((imp) => imp.exportedName);
977 if (taken.length === 0) return null;
978 for (let dir = importerDir; dir; dir = parentDir(dir)) {
979 if (dir !== importerDir && index.packageDirs.has(dir)) continue;
980 const file = pythonModuleUnder(dir, relPath, index);
981 if (!file || context.getImportMappings(file, 'python').some((imp) => imp.source === specifier)) continue;
982 const defined = new Set(context.getNodesInFile(file).filter(isPythonModuleLevelDef).map((n) => n.name));
983 if (taken.every((name) => defined.has(name))) return file;
984 }
985 return null;
986}
987
988/**
989 * A digest of the Python root set — each root with its tier and reach. A

Callers 1

resolvePythonModuleFunction · 0.85

Calls 6

pythonModuleUnderFunction · 0.85
hasMethod · 0.80
mapMethod · 0.80
getImportMappingsMethod · 0.80
parentDirFunction · 0.70
getNodesInFileMethod · 0.65

Tested by

no test coverage detected