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

Function pythonFromImports

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

2337 * which a single-line match silently dropped.
2338 */
2339function pythonFromImports(code: string): PythonFromImport[] {
2340 const out: PythonFromImport[] = [];
2341 const fromRe = /^[ \t]*from[ \t]+(\.*[\w.]*)[ \t]+import[ \t]*(\([^)]*\)|(?:[^\n\\;]|\\\r?\n)+)/gm;
2342 let m: RegExpExecArray | null;
2343 while ((m = fromRe.exec(code)) !== null) {
2344 const source = m[1]!;
2345 if (!source) continue;
2346 const list = m[2]!.replace(/^\(|\)$/g, '').replace(/\\\r?\n/g, ' ');
2347 const names: PythonFromImport['names'] = [];
2348 let star = false;
2349 for (const raw of list.split(',')) {
2350 const item = raw.trim();
2351 if (item === '*') {
2352 star = true;
2353 continue;
2354 }
2355 const im = item.match(/^(\w+)(?:\s+as\s+(\w+))?$/);
2356 if (im) names.push({ name: im[1]!, alias: im[2] ?? im[1]! });
2357 }
2358 out.push({ source, names, star });
2359 }
2360 return out;
2361}
2362
2363/**
2364 * 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