Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(gazelle) Fix dependency added as both deps and pyi_deps #3036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions gazelle/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,16 @@ func (py *Resolver) Embeds(r *rule.Rule, from label.Label) []label.Label {
}

// addDependency adds a dependency to either the regular deps or pyiDeps set based on
// whether the module is type-checking only.
func addDependency(dep string, mod Module, deps, pyiDeps *treeset.Set) {
if mod.TypeCheckingOnly {
pyiDeps.Add(dep)
// whether the module is type-checking only. If a module is added as both
// non-type-checking and type-checking, it should end up in deps and not pyiDeps.
func addDependency(dep string, typeCheckingOnly bool, deps, pyiDeps *treeset.Set) {
if typeCheckingOnly {
if !deps.Contains(dep) {
pyiDeps.Add(dep)
}
} else {
deps.Add(dep)
pyiDeps.Remove(dep)
}
}

Expand Down Expand Up @@ -240,7 +244,7 @@ func (py *Resolver) Resolve(
override.Repo = ""
}
dep := override.Rel(from.Repo, from.Pkg).String()
addDependency(dep, mod, deps, pyiDeps)
addDependency(dep, mod.TypeCheckingOnly, deps, pyiDeps)
if explainDependency == dep {
log.Printf("Explaining dependency (%s): "+
"in the target %q, the file %q imports %q at line %d, "+
Expand All @@ -251,7 +255,7 @@ func (py *Resolver) Resolve(
}
} else {
if dep, distributionName, ok := cfg.FindThirdPartyDependency(moduleName); ok {
addDependency(dep, mod, deps, pyiDeps)
addDependency(dep, mod.TypeCheckingOnly, deps, pyiDeps)
// Add the type and stub dependencies if they exist.
modules := []string{
fmt.Sprintf("%s_stubs", strings.ToLower(distributionName)),
Expand All @@ -261,8 +265,8 @@ func (py *Resolver) Resolve(
}
for _, module := range modules {
if dep, _, ok := cfg.FindThirdPartyDependency(module); ok {
// Type stub packages always go to pyiDeps
pyiDeps.Add(dep)
// Type stub packages are added as type-checking only.
addDependency(dep, true, deps, pyiDeps)
}
}
if explainDependency == dep {
Expand Down Expand Up @@ -321,7 +325,7 @@ func (py *Resolver) Resolve(
}
matchLabel := filteredMatches[0].Label.Rel(from.Repo, from.Pkg)
dep := matchLabel.String()
addDependency(dep, mod, deps, pyiDeps)
addDependency(dep, mod.TypeCheckingOnly, deps, pyiDeps)
if explainDependency == dep {
log.Printf("Explaining dependency (%s): "+
"in the target %q, the file %q imports %q at line %d, "+
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gazelle:python_generation_mode package
# gazelle:python_generate_pyi_deps true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gazelle:python_generation_mode package
# gazelle:python_generate_pyi_deps true
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Overlapping deps and pyi_deps across packages

This test reproduces a case where a dependency may be added to both `deps` and
`pyi_deps`. Package `b` imports `a.foo` normally and imports `a.bar` as a
type-checking only import. The dependency on package `a` should appear only in
`deps` (and not `pyi_deps`) of package `b`.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "gazelle_python_test")
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "a",
srcs = [
"bar.py",
"foo.py",
],
visibility = ["//:__subpackages__"],
)
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "b",
srcs = ["b.py"],
visibility = ["//:__subpackages__"],
deps = ["//a"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import TYPE_CHECKING

from a import foo

if TYPE_CHECKING:
from a import bar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---