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

Function pythonFromImports

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

* The `from X import …` statements of a Python file, read over comment- and * docstring-blanked source so a usage example in a docstring binds nothing. * Handles the parenthesised multi-line list (`from .x import (\n a,\n b,\n)`, * the usual shape of a package `__init__.py`) and backslash conti

(code: string)

Source from the content-addressed store, hash-verified

2250 * which a single-line match silently dropped.
2251 */
2252function pythonFromImports(code: string): PythonFromImport[] {
2253 const out: PythonFromImport[] = [];
2254 const fromRe = /^[ \t]*from[ \t]+(\.*[\w.]*)[ \t]+import[ \t]*(\([^)]*\)|(?:[^\n\\;]|\\\r?\n)+)/gm;
2255 let m: RegExpExecArray | null;
2256 while ((m = fromRe.exec(code)) !== null) {
2257 const source = m[1]!;
2258 if (!source) continue;
2259 const list = m[2]!.replace(/^\(|\)$/g, '').replace(/\\\r?\n/g, ' ');
2260 const names: PythonFromImport['names'] = [];
2261 let star = false;
2262 for (const raw of list.split(',')) {
2263 const item = raw.trim();
2264 if (item === '*') {
2265 star = true;
2266 continue;
2267 }
2268 const im = item.match(/^(\w+)(?:\s+as\s+(\w+))?$/);
2269 if (im) names.push({ name: im[1]!, alias: im[2] ?? im[1]! });
2270 }
2271 out.push({ source, names, star });
2272 }
2273 return out;
2274}
2275
2276/**
2277 * Extract Python import mappings

Callers 2

extractPythonImportsFunction · 0.85
extractPythonReExportsFunction · 0.85

Calls 2

pushMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected