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

Function pythonModuleOfScript

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

981 * it is no root, so it is passed over.
982 */
983function pythonModuleOfScript(
984 specifier: string,
985 relPath: string,
986 fromFile: string,
987 importerDir: string,
988 index: PythonModuleIndex,
989 context: ResolutionContext
990): string | null {
991 if (PYTHON_STDLIB_MODULES.has(specifier.split('.')[0]!)) return null;
992 const taken = importsFrom(context.getImportMappings(fromFile, 'python'), specifier)
993 .filter((imp) => !imp.isNamespace)
994 .map((imp) => imp.exportedName);
995 if (taken.length === 0) return null;
996 for (let dir = importerDir; dir; dir = parentDir(dir)) {
997 if (dir !== importerDir && index.packageDirs.has(dir)) continue;
998 const file = pythonModuleUnder(dir, relPath, index);
999 if (!file || importsFrom(context.getImportMappings(file, 'python'), specifier).length > 0) continue;
1000 const defined = new Set(context.getNodesInFile(file).filter(isPythonModuleLevelDef).map((n) => n.name));
1001 if (taken.every((name) => defined.has(name))) return file;
1002 }
1003 return null;
1004}
1005
1006/**
1007 * A digest of the Python root set — each root with its tier and reach. A

Callers 1

resolvePythonModuleFunction · 0.85

Calls 7

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

Tested by

no test coverage detected