Skip to content

docs: generate docs for py_common, PyInfoBuilder APIs #2920

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 1 commit into from
May 21, 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
1 change: 1 addition & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ sphinx_stardocs(
"//python/private:builders_util_bzl",
"//python/private:py_binary_rule_bzl",
"//python/private:py_cc_toolchain_rule_bzl",
"//python/private:py_info_bzl",
"//python/private:py_library_rule_bzl",
"//python/private:py_runtime_rule_bzl",
"//python/private:py_test_rule_bzl",
Expand Down
21 changes: 20 additions & 1 deletion python/api/api.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
"""Public, analysis phase APIs for Python rules."""
"""Public, analysis phase APIs for Python rules.

To use the analyis-time API, add the attributes to your rule, then
use `py_common.get()` to get the api object:

```
load("@rules_python//python/api:api.bzl", "py_common")

def _impl(ctx):
py_api = py_common.get(ctx)

myrule = rule(
implementation = _impl,
attrs = {...} | py_common.API_ATTRS
)
```

:::{versionadded} 0.37.0
:::
"""

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

Expand Down
12 changes: 12 additions & 0 deletions python/private/api/api.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ will depend on the target that is providing the API struct.
},
)

def _py_common_typedef():
"""Typedef for py_common.

:::{field} API_ATTRS
:type: dict[str, Attribute]

The attributes that rules must have for `py_common.get()` to work.
:::

"""

def _py_common_get(ctx):
"""Get the py_common API instance.

Expand All @@ -45,6 +56,7 @@ def _py_common_get(ctx):
return ctx.attr._py_common_api[ApiImplInfo].impl

py_common = struct(
TYPEDEF = _py_common_typedef,
get = _py_common_get,
API_ATTRS = {
"_py_common_api": attr.label(
Expand Down
29 changes: 26 additions & 3 deletions python/private/api/py_common_api.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,40 @@ def _py_common_api_impl(ctx):

py_common_api = rule(
implementation = _py_common_api_impl,
doc = "Rule implementing py_common API.",
doc = "Internal Rule implementing py_common API.",
)

def _py_common_api_typedef():
"""The py_common API implementation.

An instance of this object is obtained using {obj}`py_common.get()`
"""

def _merge_py_infos(transitive, *, direct = []):
builder = PyInfoBuilder()
"""Merge PyInfo objects into a single PyInfo.

This is a convenience wrapper around {obj}`PyInfoBuilder.merge_all`. For
more control over merging PyInfo objects, use {obj}`PyInfoBuilder`.

Args:
transitive: {type}`list[PyInfo]` The PyInfo objects with info
considered indirectly provided by something (e.g. via
its deps attribute).
direct: {type}`list[PyInfo]` The PyInfo objects that are
considered directly provided by something (e.g. via
the srcs attribute).

Returns:
{type}`PyInfo` A PyInfo containing the merged values.
"""
builder = PyInfoBuilder.new()
builder.merge_all(transitive, direct = direct)
return builder.build()

# Exposed for doc generation, not directly used.
# buildifier: disable=name-conventions
PyCommonApi = struct(
TYPEDEF = _py_common_api_typedef,
merge_py_infos = _merge_py_infos,
PyInfoBuilder = PyInfoBuilder,
PyInfoBuilder = PyInfoBuilder.new,
)
2 changes: 1 addition & 1 deletion python/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def create_py_info(
transitive sources collected from dependencies (the latter is only
necessary for deprecated extra actions support).
"""
py_info = PyInfoBuilder()
py_info = PyInfoBuilder.new()
py_info.site_packages_symlinks.add(site_packages_symlinks)
py_info.direct_original_sources.add(original_sources)
py_info.direct_pyc_files.add(required_pyc_files)
Expand Down
Loading