Skip to content

Commit cd550d9

Browse files
authored
docs: generate docs for py_common, PyInfoBuilder APIs (#2920)
I wrote up the docs awhile, but didn't fully wire them through to the doc gen. Fixes some various issues with the generated docs along the way.
1 parent c678623 commit cd550d9

File tree

8 files changed

+255
-18
lines changed

8 files changed

+255
-18
lines changed

docs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ sphinx_stardocs(
113113
"//python/private:builders_util_bzl",
114114
"//python/private:py_binary_rule_bzl",
115115
"//python/private:py_cc_toolchain_rule_bzl",
116+
"//python/private:py_info_bzl",
116117
"//python/private:py_library_rule_bzl",
117118
"//python/private:py_runtime_rule_bzl",
118119
"//python/private:py_test_rule_bzl",

python/api/api.bzl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
"""Public, analysis phase APIs for Python rules."""
1+
"""Public, analysis phase APIs for Python rules.
2+
3+
To use the analyis-time API, add the attributes to your rule, then
4+
use `py_common.get()` to get the api object:
5+
6+
```
7+
load("@rules_python//python/api:api.bzl", "py_common")
8+
9+
def _impl(ctx):
10+
py_api = py_common.get(ctx)
11+
12+
myrule = rule(
13+
implementation = _impl,
14+
attrs = {...} | py_common.API_ATTRS
15+
)
16+
```
17+
18+
:::{versionadded} 0.37.0
19+
:::
20+
"""
221

322
load("//python/private/api:api.bzl", _py_common = "py_common")
423

python/private/api/api.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ will depend on the target that is providing the API struct.
2727
},
2828
)
2929

30+
def _py_common_typedef():
31+
"""Typedef for py_common.
32+
33+
:::{field} API_ATTRS
34+
:type: dict[str, Attribute]
35+
36+
The attributes that rules must have for `py_common.get()` to work.
37+
:::
38+
39+
"""
40+
3041
def _py_common_get(ctx):
3142
"""Get the py_common API instance.
3243
@@ -45,6 +56,7 @@ def _py_common_get(ctx):
4556
return ctx.attr._py_common_api[ApiImplInfo].impl
4657

4758
py_common = struct(
59+
TYPEDEF = _py_common_typedef,
4860
get = _py_common_get,
4961
API_ATTRS = {
5062
"_py_common_api": attr.label(

python/private/api/py_common_api.bzl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,40 @@ def _py_common_api_impl(ctx):
2222

2323
py_common_api = rule(
2424
implementation = _py_common_api_impl,
25-
doc = "Rule implementing py_common API.",
25+
doc = "Internal Rule implementing py_common API.",
2626
)
2727

28+
def _py_common_api_typedef():
29+
"""The py_common API implementation.
30+
31+
An instance of this object is obtained using {obj}`py_common.get()`
32+
"""
33+
2834
def _merge_py_infos(transitive, *, direct = []):
29-
builder = PyInfoBuilder()
35+
"""Merge PyInfo objects into a single PyInfo.
36+
37+
This is a convenience wrapper around {obj}`PyInfoBuilder.merge_all`. For
38+
more control over merging PyInfo objects, use {obj}`PyInfoBuilder`.
39+
40+
Args:
41+
transitive: {type}`list[PyInfo]` The PyInfo objects with info
42+
considered indirectly provided by something (e.g. via
43+
its deps attribute).
44+
direct: {type}`list[PyInfo]` The PyInfo objects that are
45+
considered directly provided by something (e.g. via
46+
the srcs attribute).
47+
48+
Returns:
49+
{type}`PyInfo` A PyInfo containing the merged values.
50+
"""
51+
builder = PyInfoBuilder.new()
3052
builder.merge_all(transitive, direct = direct)
3153
return builder.build()
3254

3355
# Exposed for doc generation, not directly used.
3456
# buildifier: disable=name-conventions
3557
PyCommonApi = struct(
58+
TYPEDEF = _py_common_api_typedef,
3659
merge_py_infos = _merge_py_infos,
37-
PyInfoBuilder = PyInfoBuilder,
60+
PyInfoBuilder = PyInfoBuilder.new,
3861
)

python/private/common.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def create_py_info(
405405
transitive sources collected from dependencies (the latter is only
406406
necessary for deprecated extra actions support).
407407
"""
408-
py_info = PyInfoBuilder()
408+
py_info = PyInfoBuilder.new()
409409
py_info.site_packages_symlinks.add(site_packages_symlinks)
410410
py_info.direct_original_sources.add(original_sources)
411411
py_info.direct_pyc_files.add(required_pyc_files)

0 commit comments

Comments
 (0)