Skip to content

Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.

fix(import-map): resolve `from . import NAME` to the package __init__.py

drakeo33808d16bd65e97
diff --git a/tests/skill/understand/test_extract_import_map.test.mjs b/tests/skill/understand/test_extract_import_map.test.mjs
index 5d55c489..5680d346 100644
--- a/tests/skill/understand/test_extract_import_map.test.mjs
+++ b/tests/skill/understand/test_extract_import_map.test.mjs
@@ -713,6 +713,49 @@ describe('extract-import-map.mjs — Python resolver', () => {
]);
});
+ it('falls back to the package __init__.py for `from . import NAME` / `from .. import NAME` (#727)', () => {
+ projectRoot = setupTree({
+ 'pkg/__init__.py': `class BaseAnalyzer:\n pass\n`,
+ 'pkg/stats/__init__.py': `LIMIT = 10\n`,
+ 'pkg/core/__init__.py': `def boot():\n return 1\n`,
+ 'pkg/stats/regression.py':
+ `from .. import BaseAnalyzer\nfrom . import LIMIT\nfrom ..core import boot\n`,
+ 'pkg/stats/other.py': `from . import regression, LIMIT, MORE\n`,
+ 'lone/__init__.py': `from . import NAME\n`,
+ 'pkg/core/star.py': `from .. import *\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ 'pkg/__init__.py',
+ 'pkg/stats/__init__.py',
+ 'pkg/core/__init__.py',
+ 'pkg/stats/regression.py',
+ 'pkg/stats/other.py',
+ 'lone/__init__.py',
+ 'pkg/core/star.py',
+ ].map(path => ({ path, language: 'python', fileCategory: 'code' })),
+ });
+
+ expect(result.status).toBe(0);
+ // `from .. import *` binds from the parent package's __init__.py.
+ expect(result.output.importMap['pkg/core/star.py']).toEqual(['pkg/__init__.py']);
+ // The importer is its own package `__init__.py`: no self-edge.
+ expect(result.output.importMap['lone/__init__.py'] ?? []).toEqual([]);
+ expect(result.output.importMap['pkg/stats/regression.py']).toEqual([
+ 'pkg/__init__.py',
+ 'pkg/core/__init__.py',
+ 'pkg/stats/__init__.py',
+ ]);
+ // A submodule match still wins; the unresolved names collapse to one
+ // edge to the package __init__.py.
+ expect(result.output.importMap['pkg/stats/other.py']).toEqual([
+ 'pkg/stats/__init__.py',
+ 'pkg/stats/regression.py',
+ ]);
+ });
+
// Regression for Codex review #2 on PR #204: `from . import x` was
// dropped when no `__init__.py` was present at the importer's package
// dir, because resolvePythonProbe gated specifier probing on the package
diff --git a/understand-anything-plugin/skills/understand/extract-import-map.mjs b/understand-anything-plugin/skills/understand/extract-import-map.mjs
index 84b3a6d7..c45073a5 100644
--- a/understand-anything-plugin/skills/understand/extract-import-map.mjs
+++ b/understand-anything-plugin/skills/understand/extract-import-map.mjs
@@ -978,15 +978,26 @@ export function resolvePythonImport(rawImport, specifiers, file, ctx) {
if (tailSegments.length === 0) {
if (!Array.isArray(specifiers) || specifiers.length === 0) return [];
const base = baseParts.join('/');
+ const pkgInit = base ? `${base}/__init__.py` : '__init__.py';
const matches = [];
for (const spec of specifiers) {
- // Wildcard `*` and qualified specifiers (`Foo.bar`) skip; the
- // surface name is what tree-sitter records for `from . import x`.
- if (!spec || spec === '*' || spec.includes('.')) continue;
+ // Qualified specifiers (`Foo.bar`) skip; the surface name is what
+ // tree-sitter records for `from . import x`.
+ if (!spec || spec.includes('.')) continue;
+ const usePkgInit =
+ ctx.fileSet.has(pkgInit) && pkgInit !== toPosix(file.path) && !matches.includes(pkgInit);
+ // `from . import *` binds the package's own names from `__init__.py`.
+ if (spec === '*') {
+ if (usePkgInit) matches.push(pkgInit);
+ continue;
+ }
const subFile = base ? `${base}/${spec}.py` : `${spec}.py`;
const subInit = base ? `${base}/${spec}/__init__.py` : `${spec}/__init__.py`;
if (ctx.fileSet.has(subFile)) matches.push(subFile);
else if (ctx.fileSet.has(subInit)) matches.push(subInit);
+ // Not a submodule, so the name is defined in (or re-exported by)
+ // the package itself: `from . import NAME` binds from `__init__.py`.
+ else if (usePkgInit) matches.push(pkgInit);
}
return matches;
}