Skip to content

fix: resolve Python dot-notation imports in dependency graph#8

Merged
thegdsks merged 1 commit intomasterfrom
fix/python-import-resolution
Apr 11, 2026
Merged

fix: resolve Python dot-notation imports in dependency graph#8
thegdsks merged 1 commit intomasterfrom
fix/python-import-resolution

Conversation

@thegdsks
Copy link
Copy Markdown
Member

Summary

  • Python relative imports (.models, ..utils) and dotted absolute imports (myapp.db.client) were silently dropped during graph building
  • resolveImport only handled JS/Go-style path separators (./foo, ../foo)
  • Added normalizePythonImport() to convert dot-prefix notation to path-style before resolution
  • Dotted absolute imports now get slash-converted for module matching

Test plan

  • Unit tests for single-dot relative imports (.models)
  • Unit tests for parent-relative imports (..db)
  • Unit tests for dotted absolute imports (myapp.db.client)
  • Integration test with multi-package Python project structure
  • Full regression suite passes (go test ./...)

Fixes #7

Python relative imports (.models, ..utils) and dotted absolute imports
(myapp.db.client) were silently dropped because resolveImport only
handled path-style separators (./foo, ../foo). Adds normalizePythonImport
to convert dot-prefix to path-style, and slash-converts dotted package
imports for module matching.

Fixes #7
Copilot AI review requested due to automatic review settings April 11, 2026 20:35
@thegdsks thegdsks merged commit 5708dd5 into master Apr 11, 2026
3 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Stacklit’s dependency graph resolution for Python imports that use dot-notation (relative .foo, ..bar) and dotted absolute imports (pkg.subpkg.module) so they can be matched to known modules during graph building (Fixes #7).

Changes:

  • Normalize Python dot-prefix relative imports into path-style before resolution.
  • Add dotted-import matching support by converting . to / during module matching.
  • Add graph-level unit tests covering Python parent-relative and dotted absolute imports (plus a relative import test).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
stacklit.json Regenerated project metadata snapshot.
internal/graph/graph.go Adds Python import normalization and dotted-import matching in resolveImport.
internal/graph/graph_test.go Adds tests intended to cover Python import resolution behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +313 to +314
// Also convert dotted imports (Python "os.path" → "os/path") for matching.
slashImp := strings.ReplaceAll(imp, ".", "/")
Comment on lines 316 to 330
for _, mod := range knownModules {
if mod == imp {
if mod == imp || mod == slashImp {
return mod
}
// Suffix match: "internal/auth" matches module "internal/auth".
if strings.HasSuffix(imp, "/"+mod) || strings.HasSuffix(imp, mod) {
if strings.HasSuffix(imp, "/"+mod) || strings.HasSuffix(slashImp, "/"+mod) {
return mod
}
// Module is a prefix of the import path (package.submodule → package/).
if strings.HasPrefix(slashImp, mod+"/") || strings.HasPrefix(imp, mod+"/") {
return mod
}
// Module is a suffix of the import path (Go-style).
if strings.HasSuffix(imp, mod) || imp == mod {
if strings.HasSuffix(imp, mod) || strings.HasSuffix(slashImp, mod) {
return mod
Comment on lines +147 to +168
// TestPythonRelativeImports verifies that Python dot-notation relative imports
// produce correct dependency edges.
func TestPythonRelativeImports(t *testing.T) {
files := []*parser.FileInfo{
{Path: "myapp/api/views.py", Language: "python", Imports: []string{"flask", ".models", ".config"}, LineCount: 30},
{Path: "myapp/api/models.py", Language: "python", Exports: []string{"User", "Post"}, LineCount: 20},
{Path: "myapp/api/config.py", Language: "python", Exports: []string{"Settings"}, LineCount: 10},
{Path: "myapp/db/client.py", Language: "python", Exports: []string{"get_conn"}, LineCount: 15},
}

g := Build(files, BuildOptions{MaxDepth: 4})

apiMod := g.Module("myapp/api")
if apiMod == nil {
t.Fatal("expected module myapp/api to exist")
}

// ".models" and ".config" are in the same directory so they resolve to same module.
// They should NOT create self-edges. External "flask" should not appear.
if len(apiMod.DependsOn) != 0 {
t.Errorf("expected no external deps (self-refs filtered), got %v", apiMod.DependsOn)
}

// Relative imports: ./foo or ../foo
if strings.HasPrefix(imp, "./") || strings.HasPrefix(imp, "../") {
resolved := filepath.Clean(filepath.Join(fileDir, imp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cant generate dependencies

2 participants