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

Function pythonFromImports

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

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