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

Function pythonPackageImporters

src/resolution/import-resolver.ts:1104–1158  ·  view source on GitHub ↗
(
  context: ResolutionContext,
  initFiles: string[]
)

Source from the content-addressed store, hash-verified

1102 * its own importers are included too, to a fixed point.
1103 */
1104export function pythonPackageImporters(
1105 context: ResolutionContext,
1106 initFiles: string[]
1107): Map<string, Map<string, string | null>> {
1108 const index = pythonModuleIndex(context);
1109 const packages = new Set(initFiles.map((f) => f.replace(/\\/g, '/')));
1110 const out = new Map<string, Map<string, string | null>>();
1111 const bindingNow = (imp: ImportMapping, file: string): string | null => {
1112 if (imp.isNamespace || imp.exportedName === '*') return null;
1113 const packageFile = resolveImportPath(imp.source, file, 'python', context);
1114 if (!packageFile) return null;
1115 const symbol = findExportedSymbol(
1116 packageFile,
1117 { isDefault: false, isNamespace: false, exportedName: imp.exportedName, memberName: null },
1118 'python',
1119 context,
1120 new Set()
1121 );
1122 if (symbol) return `${symbol.filePath}\0${symbol.name}`;
1123 const sub = resolveImportPath(
1124 imp.source.endsWith('.') ? imp.source + imp.exportedName : `${imp.source}.${imp.exportedName}`,
1125 file,
1126 'python',
1127 context
1128 );
1129 return sub ? `${sub}\0` : '\0';
1130 };
1131 // Candidate importers from the import nodes (named by their specifier),
1132 // so only files that import an edited package pay for a mapping parse.
1133 const importNodes = context.getNodesByKind('import').filter((n) => PYTHON_MODULE_FILE.test(n.filePath));
1134 for (let grew = true; grew; ) {
1135 grew = false;
1136 const candidates = new Set<string>();
1137 for (const n of importNodes) {
1138 const target = resolveImportPath(n.name, n.filePath, 'python', context);
1139 if (target && packages.has(target) && index.files.has(n.filePath)) candidates.add(n.filePath);
1140 }
1141 for (const file of candidates) {
1142 for (const imp of context.getImportMappings(file, 'python')) {
1143 const target = resolveImportPath(imp.source, file, 'python', context);
1144 if (!target || !packages.has(target) || target === file) continue;
1145 let names = out.get(file);
1146 if (!names) out.set(file, (names = new Map()));
1147 names.set(imp.localName, bindingNow(imp, file));
1148 // `import pkg.sub` is used as `pkg.sub.N`.
1149 if (imp.isNamespace) names.set(imp.source.split('.')[0]!, null);
1150 if (/(?:^|\/)__init__\.py$/.test(file) && !packages.has(file)) {
1151 packages.add(file);
1152 grew = true;
1153 }
1154 }
1155 }
1156 }
1157 return out;
1158}
1159
1160/** The file node of the module `specifier` names, as seen from `fromFile`. */
1161function pythonModuleFileNode(specifier: string, fromFile: string, context: ResolutionContext): Node | null {

Callers 1

Calls 10

pythonModuleIndexFunction · 0.85
resolveImportPathFunction · 0.85
bindingNowFunction · 0.85
mapMethod · 0.80
hasMethod · 0.80
getImportMappingsMethod · 0.80
getNodesByKindMethod · 0.65
getMethod · 0.65
addMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected