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

Function pythonModuleOfScript

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

979 * it is no root, so it is passed over.
980 */
981function pythonModuleOfScript(
982 specifier: string,
983 relPath: string,
984 fromFile: string,
985 importerDir: string,
986 index: PythonModuleIndex,
987 context: ResolutionContext
988): string | null {
989 if (PYTHON_STDLIB_MODULES.has(specifier.split('.')[0]!)) return null;
990 const taken = context.getImportMappings(fromFile, 'python')
991 .filter((imp) => imp.source === specifier && !imp.isNamespace)
992 .map((imp) => imp.exportedName);
993 if (taken.length === 0) return null;
994 for (let dir = importerDir; dir; dir = parentDir(dir)) {
995 if (dir !== importerDir && index.packageDirs.has(dir)) continue;
996 const file = pythonModuleUnder(dir, relPath, index);
997 if (!file || context.getImportMappings(file, 'python').some((imp) => imp.source === specifier)) continue;
998 const defined = new Set(context.getNodesInFile(file).filter(isPythonModuleLevelDef).map((n) => n.name));
999 if (taken.every((name) => defined.has(name))) return file;
1000 }
1001 return null;
1002}
1003
1004/**
1005 * 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