Skip to content

Commit a00ec81

Browse files
committed
Add new test case for siblings imports disabled in file mode
1 parent 29e815a commit a00ec81

File tree

17 files changed

+106
-6
lines changed

17 files changed

+106
-6
lines changed

gazelle/python/resolve.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,14 @@ func (py *Resolver) Resolve(
164164
modules := modulesRaw.(*treeset.Set)
165165
it := modules.Iterator()
166166
explainDependency := os.Getenv("EXPLAIN_DEPENDENCY")
167-
// Resolve relative paths for package generation
168-
isPackageGeneration := !cfg.PerFileGeneration() && !cfg.CoarseGrainedGeneration()
169167
hasFatalError := false
170168
MODULES_LOOP:
171169
for it.Next() {
172170
mod := it.Value().(Module)
173171
moduleName := mod.Name
174172
// Transform relative imports `.` or `..foo.bar` into the package path from root.
175173
if strings.HasPrefix(mod.From, ".") {
176-
if !cfg.ExperimentalAllowRelativeImports() || !isPackageGeneration {
174+
if !cfg.ExperimentalAllowRelativeImports() {
177175
continue MODULES_LOOP
178176
}
179177

@@ -210,9 +208,9 @@ func (py *Resolver) Resolve(
210208
baseParts = pkgParts[:len(pkgParts)-(relativeDepth-1)]
211209
}
212210
// Build absolute module path
213-
absParts := append([]string{}, baseParts...) // base path
214-
absParts = append(absParts, fromParts...) // subpath from 'from'
215-
absParts = append(absParts, imported) // actual imported symbol
211+
absParts := append([]string{}, baseParts...) // base path
212+
absParts = append(absParts, fromParts...) // subpath from 'from'
213+
absParts = append(absParts, imported) // actual imported symbol
216214

217215
moduleName = strings.Join(absParts, ".")
218216
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# gazelle:python_generation_mode file
2+
# gazelle:python_resolve_sibling_imports false
3+
# gazelle:experimental_allow_relative_imports true
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
2+
3+
# gazelle:python_generation_mode file
4+
# gazelle:python_resolve_sibling_imports false
5+
# gazelle:experimental_allow_relative_imports true
6+
7+
py_library(
8+
name = "a",
9+
srcs = ["a.py"],
10+
visibility = ["//:__subpackages__"],
11+
)
12+
13+
py_library(
14+
name = "b",
15+
srcs = ["b.py"],
16+
visibility = ["//:__subpackages__"],
17+
)
18+
19+
py_test(
20+
name = "test_util",
21+
srcs = ["test_util.py"],
22+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sibling imports disabled (file generation mode)
2+
3+
This test case asserts that imports from sibling modules are NOT resolved as
4+
absolute imports when the `python_resolve_sibling_imports` directive is
5+
disabled. It covers different types of imports in `pkg/unit_test.py`:
6+
7+
- `import a` - resolves to the root-level `a.py` instead of the sibling
8+
`pkg/a.py`
9+
- `from typing import Iterable` - resolves to the stdlib `typing` module
10+
(not the sibling `typing.py`).
11+
- `from .b import run` / `from .typing import A` - resolves to the sibling
12+
`pkg/b.py` / `pkg/typing.py` (with
13+
`gazelle:experimental_allow_relative_imports` enabled)
14+
- `import test_util` - resolves to the root-level `test_util.py` instead of
15+
the sibling `pkg/test_util.py`
16+
- `from b import run` - resolves to the root-level `b.py` instead of the
17+
sibling `pkg/b.py`
18+
19+
When sibling imports are disabled with
20+
`# gazelle:python_resolve_sibling_imports false`, the imports remain as-is
21+
and follow standard Python resolution rules where absolute imports can't refer
22+
to sibling modules.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Root level a.py file for testing disabled sibling imports
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Root level b.py file for testing disabled sibling imports
2+
def run():
3+
pass

gazelle/python/testdata/sibling_imports_disabled_file_mode/pkg/BUILD.in

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
2+
3+
py_library(
4+
name = "a",
5+
srcs = ["a.py"],
6+
visibility = ["//:__subpackages__"],
7+
)
8+
9+
py_library(
10+
name = "b",
11+
srcs = ["b.py"],
12+
visibility = ["//:__subpackages__"],
13+
)
14+
15+
py_library(
16+
name = "typing",
17+
srcs = ["typing.py"],
18+
visibility = ["//:__subpackages__"],
19+
)
20+
21+
py_test(
22+
name = "test_util",
23+
srcs = ["test_util.py"],
24+
deps = [
25+
":b",
26+
":typing",
27+
],
28+
)
29+
30+
py_test(
31+
name = "unit_test",
32+
srcs = ["unit_test.py"],
33+
deps = [
34+
"//:a",
35+
"//:b",
36+
"//:test_util",
37+
],
38+
)

gazelle/python/testdata/sibling_imports_disabled_file_mode/pkg/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)