fix: resolve Python dot-notation imports in dependency graph#8
Merged
fix: resolve Python dot-notation imports in dependency graph#8
Conversation
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
There was a problem hiding this comment.
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
.models,..utils) and dotted absolute imports (myapp.db.client) were silently dropped during graph buildingresolveImportonly handled JS/Go-style path separators (./foo,../foo)normalizePythonImport()to convert dot-prefix notation to path-style before resolutionTest plan
.models)..db)myapp.db.client)go test ./...)Fixes #7