Skip to content

Commit 37fbdc0

Browse files
committed
Propagate .pyi files via direct_pyi_files and transitive_pyi_files
Per reviewer feedback, we here propagate the paths to the `.pyi` files via the `direct_pyi_files` and `transitive_pyi_files` members of `PyInfo` which are available starting in `rules_python` 1.1.0. Since this means that the files are not automatically in the runfiles of the `py_proto_library`, tooling to run mypy will need to be adjusted to add what is needed from these provider fields to the runfiles if that is what is wanted.
1 parent 827c89c commit 37fbdc0

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

bazel/py_proto_library.bzl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ _PyProtoInfo = provider(
1717
(depset[File]) Files from the transitive closure implicit proto
1818
dependencies""",
1919
"transitive_sources": """(depset[File]) The Python sources.""",
20+
"direct_pyi_files": """
21+
(depset[File]) Type definition files (usually `.pyi` files)
22+
for the Python modules provided by this target.""",
23+
"transitive_pyi_files": """
24+
(depset[File]) The transitive set of type definition files
25+
(usually `.pyi` files) for the Python modules for this target
26+
and its transitive dependencies.""",
2027
},
2128
)
2229

@@ -107,7 +114,7 @@ def _py_proto_aspect_impl(target, ctx):
107114
)
108115

109116
# Generated sources == Python sources
110-
python_sources = generated_sources + generated_stubs
117+
python_sources = generated_sources
111118

112119
deps = _filter_provider(_PyProtoInfo, getattr(_proto_library, "deps", []))
113120
runfiles_from_proto_deps = depset(
@@ -118,6 +125,13 @@ def _py_proto_aspect_impl(target, ctx):
118125
direct = python_sources,
119126
transitive = [dep.transitive_sources for dep in deps],
120127
)
128+
direct_pyi_files = depset(
129+
direct = generated_stubs,
130+
)
131+
transitive_pyi_files = depset(
132+
direct = generated_stubs,
133+
transitive = [dep.transitive_pyi_files for dep in deps],
134+
)
121135

122136
return [
123137
_PyProtoInfo(
@@ -133,6 +147,8 @@ def _py_proto_aspect_impl(target, ctx):
133147
),
134148
runfiles_from_proto_deps = runfiles_from_proto_deps,
135149
transitive_sources = transitive_sources,
150+
direct_pyi_files = direct_pyi_files,
151+
transitive_pyi_files = transitive_pyi_files,
136152
),
137153
]
138154

@@ -164,6 +180,13 @@ def _py_proto_library_rule(ctx):
164180
default_outputs = depset(
165181
transitive = [info.transitive_sources for info in pyproto_infos],
166182
)
183+
direct_pyi_files = []
184+
for info in pyproto_infos:
185+
direct_pyi_files.extend(info.direct_pyi_files.to_list())
186+
transitive_pyi_files = depset(
187+
direct = direct_pyi_files,
188+
transitive = [info.transitive_pyi_files for info in pyproto_infos],
189+
)
167190

168191
return [
169192
DefaultInfo(
@@ -180,6 +203,8 @@ def _py_proto_library_rule(ctx):
180203
PyInfo(
181204
transitive_sources = default_outputs,
182205
imports = depset(transitive = [info.imports for info in pyproto_infos]),
206+
direct_pyi_files = depset(direct = direct_pyi_files),
207+
transitive_pyi_files = transitive_pyi_files,
183208
# Proto always produces 2- and 3- compatible source files
184209
has_py2_only_sources = False,
185210
has_py3_only_sources = False,

0 commit comments

Comments
 (0)