@@ -39,6 +39,9 @@ const (
3939 // resolvedDepsKey is the attribute key used to pass dependencies that don't
4040 // need to be resolved by the dependency resolver in the Resolver step.
4141 resolvedDepsKey = "_gazelle_python_resolved_deps"
42+ // resolvedPyiDepsKey is the attribute key used to pass type-checking dependencies that don't
43+ // need to be resolved by the dependency resolver in the Resolver step.
44+ resolvedPyiDepsKey = "_gazelle_python_resolved_pyi_deps"
4245)
4346
4447// Resolver satisfies the resolve.Resolver interface. It resolves dependencies
@@ -123,6 +126,16 @@ func (py *Resolver) Embeds(r *rule.Rule, from label.Label) []label.Label {
123126 return make ([]label.Label , 0 )
124127}
125128
129+ // addDependency adds a dependency to either the regular deps or pyiDeps set based on
130+ // whether the module is type-checking only.
131+ func addDependency (dep string , mod Module , deps , pyiDeps * treeset.Set ) {
132+ if mod .TypeCheckingOnly {
133+ pyiDeps .Add (dep )
134+ } else {
135+ deps .Add (dep )
136+ }
137+ }
138+
126139// Resolve translates imported libraries for a given rule into Bazel
127140// dependencies. Information about imported libraries is returned for each
128141// rule generated by language.GenerateRules in
@@ -141,6 +154,8 @@ func (py *Resolver) Resolve(
141154 // join with the main Gazelle binary with other rules. It may conflict with
142155 // other generators that generate py_* targets.
143156 deps := treeset .NewWith (godsutils .StringComparator )
157+ pyiDeps := treeset .NewWith (godsutils .StringComparator )
158+
144159 if modulesRaw != nil {
145160 cfgs := c .Exts [languageName ].(pythonconfig.Configs )
146161 cfg := cfgs [from .Pkg ]
@@ -179,7 +194,7 @@ func (py *Resolver) Resolve(
179194 override .Repo = ""
180195 }
181196 dep := override .Rel (from .Repo , from .Pkg ).String ()
182- deps . Add (dep )
197+ addDependency (dep , mod , deps , pyiDeps )
183198 if explainDependency == dep {
184199 log .Printf ("Explaining dependency (%s): " +
185200 "in the target %q, the file %q imports %q at line %d, " +
@@ -190,7 +205,7 @@ func (py *Resolver) Resolve(
190205 }
191206 } else {
192207 if dep , distributionName , ok := cfg .FindThirdPartyDependency (moduleName ); ok {
193- deps . Add (dep )
208+ addDependency (dep , mod , deps , pyiDeps )
194209 // Add the type and stub dependencies if they exist.
195210 modules := []string {
196211 fmt .Sprintf ("%s_stubs" , strings .ToLower (distributionName )),
@@ -200,7 +215,8 @@ func (py *Resolver) Resolve(
200215 }
201216 for _ , module := range modules {
202217 if dep , _ , ok := cfg .FindThirdPartyDependency (module ); ok {
203- deps .Add (dep )
218+ // Type stub packages always go to pyiDeps
219+ pyiDeps .Add (dep )
204220 }
205221 }
206222 if explainDependency == dep {
@@ -259,7 +275,7 @@ func (py *Resolver) Resolve(
259275 }
260276 matchLabel := filteredMatches [0 ].Label .Rel (from .Repo , from .Pkg )
261277 dep := matchLabel .String ()
262- deps . Add (dep )
278+ addDependency (dep , mod , deps , pyiDeps )
263279 if explainDependency == dep {
264280 log .Printf ("Explaining dependency (%s): " +
265281 "in the target %q, the file %q imports %q at line %d, " +
@@ -284,15 +300,29 @@ func (py *Resolver) Resolve(
284300 os .Exit (1 )
285301 }
286302 }
287- resolvedDeps := r .PrivateAttr (resolvedDepsKey ).(* treeset.Set )
303+
304+ addResolvedDepsAndSetAttr (r , deps , resolvedDepsKey , "deps" )
305+ addResolvedDepsAndSetAttr (r , pyiDeps , resolvedPyiDepsKey , "pyi_deps" )
306+ }
307+
308+ // addResolvedDepsAndSetAttr adds the pre-resolved dependencies from the rule's private attributes
309+ // to the provided deps set and sets the attribute on the rule.
310+ func addResolvedDepsAndSetAttr (
311+ r * rule.Rule ,
312+ deps * treeset.Set ,
313+ resolvedDepsAttrName string ,
314+ depsAttrName string ,
315+ ) {
316+ resolvedDeps := r .PrivateAttr (resolvedDepsAttrName ).(* treeset.Set )
288317 if ! resolvedDeps .Empty () {
289318 it := resolvedDeps .Iterator ()
290319 for it .Next () {
291320 deps .Add (it .Value ())
292321 }
293322 }
323+
294324 if ! deps .Empty () {
295- r .SetAttr ("deps" , convertDependencySetToExpr (deps ))
325+ r .SetAttr (depsAttrName , convertDependencySetToExpr (deps ))
296326 }
297327}
298328
0 commit comments