Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3ef53b2
refactor(pypi): select packages from the pyproject.toml dependency gr…
aignas Nov 7, 2026
7557370
refactor(pypi): support hiding the unexposed packages of a hub
aignas Nov 7, 2026
23127e5
feat(pypi): add pip.configure to select the exposed packages of a hub
aignas Nov 7, 2026
6bb7f83
fix(pypi): only add the exposed packages of a configured hub to @pypi
aignas Nov 7, 2026
fcaac66
refactor(pypi): support testonly packages in the hub repositories
aignas Nov 7, 2026
0630613
feat(pypi): add pip.configure.testonly to make packages testonly
aignas Nov 7, 2026
b31a734
fix(pypi): treat exposing all packages as not configuring the hub
aignas Nov 8, 2026
9d97360
refactor(pypi): use explicit loops and simpler conditions in pip.conf…
aignas Nov 8, 2026
35c0660
test(pypi): test the pip.default pyproject_toml with pip.configure
aignas Nov 8, 2026
ffa3e55
Merge branch 'aignas.feat.dep-group-selectors' into aignas.feat.pip-c…
aignas Nov 8, 2026
003655c
refactor(pypi): drop the unreachable testonly check in pkg_aliases
aignas Nov 8, 2026
5e4c809
refactor(pypi): compute the testonly packages once in parse_modules
aignas Nov 8, 2026
41a0664
news: name the pip.configure.testonly entry after the PR
aignas Nov 8, 2026
71be0c0
fix(pypi): only pass testonly to the package alias when it is set
aignas Nov 8, 2026
a2049ee
Merge branch 'main' into aignas.feat.dep-group-selectors
aignas Nov 9, 2026
330eb71
Merge remote-tracking branch 'upstream/main' into aignas.feat.dep-gro…
aignas Nov 15, 2026
3887ace
Merge remote-tracking branch 'upstream/main' into aignas.feat.pip-con…
aignas Nov 15, 2026
7765a28
Merge remote-tracking branch 'upstream/main' into aignas.feat.dep-gro…
aignas Nov 17, 2026
568c362
Merge branch 'aignas.feat.dep-group-selectors' into aignas.feat.pip-c…
aignas Nov 17, 2026
1ffebb8
refactor(pypi): polish pip.configure docs, docstrings, and formatting
rickeylev Nov 19, 2026
a40d1d5
refactor(pypi): restore pyproject_toml attribute docstrings
rickeylev Nov 19, 2026
1291919
refactor(pypi): short-circuit _fail paths with None in group selection
rickeylev Nov 19, 2026
28b48d5
refactor(pypi): expand nested self-extras and refine type comments
rickeylev Nov 19, 2026
0f35bd1
feat(pypi): make pip.configure a builder that does not require a pypr…
aignas Nov 19, 2026
828be47
Merge remote-tracking branch 'origin/aignas.feat.dep-group-selectors'…
aignas Nov 19, 2026
591ccf1
fix(pypi): fail on pip.configure.testonly without the dependency groups
aignas Nov 19, 2026
7c58d7b
docs(pypi): rewrap the pip.configure docs
aignas Nov 19, 2026
ddd8de2
Merge remote-tracking branch 'origin/aignas.feat.dep-group-selectors'…
aignas Nov 19, 2026
18258a4
fix(pypi): let the packages depend on the testonly packages of the hub
aignas Nov 22, 2026
8ba32e4
Merge remote-tracking branch 'upstream/main' into aignas.feat.pip-con…
aignas Nov 22, 2026
fe45b4e
fix(pypi): substitute the testonly packages in the WORKSPACE config repo
aignas Nov 22, 2026
bc5c3a5
fix(pypi): only mark the packages as testonly in the unified hub if a…
aignas Nov 23, 2026
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
29 changes: 29 additions & 0 deletions docs/pypi/download.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,35 @@ the `pyproject.toml` selects the optional dependencies of the project. The
packages that are not part of the hub are ignored, so a single `pyproject.toml`
can list tools that are missing from the lock file.

### Testonly Packages

Set {attr}`pip.configure.testonly` to make the selected packages testonly, so
that they can only be used by tests and other testonly targets:

```starlark
pip.configure(
hub_name = "pypi_hub",
dependency_groups = ["default"],
)
pip.configure(
hub_name = "pypi_hub",
dependency_groups = ["group:test"],
testonly = True,
)
```

A package that is also selected by a tag without `testonly` is not testonly.
The testonly packages are left out of the `all_requirements`,
`all_whl_requirements` and `all_data_requirements` lists in the hub's
`requirements.bzl` and are listed in `all_testonly_requirements` instead. In
the [Unified @pypi Hub](unified-pypi-hub), the targets of a package point to
the hub that is selected, so the package is testonly when it is testonly in
that hub.

Only the targets of the hub are testonly. The other packages of the hub can
depend on the testonly packages, so a testonly package is still a transitive
dependency of the packages that need it, even if they are not testonly.

:::{versionadded} VERSION_NEXT_FEATURE
:::

Expand Down
3 changes: 3 additions & 0 deletions news/4428.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(pypi) Added {attr}`pip.configure.testonly` to make the packages selected by
{obj}`pip.configure` testonly
([#3413](https://github.com/bazel-contrib/rules_python/issues/3413)).
1 change: 1 addition & 0 deletions python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ bzl_library(
deps = [
":labels",
":whl_library_deps_targets",
"//python:py_library",
"//python/private:common_labels",
"//python/private:text_util",
"@bazel_skylib//lib:selects",
Expand Down
2 changes: 2 additions & 0 deletions python/private/pypi/config.bzl.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ with your usecase. This may change in between rules_python versions without any
"""

packages = %%PACKAGES%%

testonly_packages = %%TESTONLY_PACKAGES%%
57 changes: 49 additions & 8 deletions python/private/pypi/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,30 @@ You cannot use both the additive_build_content and additive_build_content_file a
# The `all_*requirements` only list the selected packages that are
# available on all of the platforms, while all of the selected packages
# stay public.
for hub_name, packages in configured_hubs.items():
selected = {name: None for name in packages}
testonly_packages = {}
unified_testonly_packages = {}
for hub_name, info in configured_hubs.items():
selected = {name: None for name in info.packages}
exposed_packages[hub_name] = [
name
for name in exposed_packages[hub_name]
if name in selected
]
testonly_packages[hub_name] = info.testonly_packages

# The unified hub only has aliases and `testonly` is enforced by the
# targets of the hubs that they point to, so a package is only marked as
# testonly there if it is testonly in all of the hubs that have it.
for hub_name, hub in hubs.items():
info = configured_hubs.get(hub_name)
hub_testonly = {pkg: None for pkg in info.testonly_packages} if info else {}
for pkg in (info.packages if info else hub.packages):
unified_testonly_packages[pkg] = (
unified_testonly_packages.get(pkg, True) and pkg in hub_testonly
)

return struct(
# dict[str, list[str]]: configured hubs and exposed packages.
# dict[str, struct]: configured hubs, see `hub_exposure`.
configured_hubs = configured_hubs,
config = config,
declared_deps = declared_deps,
Expand All @@ -409,7 +423,13 @@ You cannot use both the additive_build_content and additive_build_content_file a
facts = simpleapi_cache.get_facts(),
hub_group_map = hub_group_map,
hub_whl_map = hub_whl_map,
testonly_packages = testonly_packages,
unified_hub_requirements_bzl = config.unified_hub_requirements_bzl,
unified_testonly_packages = sorted([
pkg
for pkg, testonly in unified_testonly_packages.items()
if testonly
]),
whl_libraries = whl_libraries,
whl_mods = whl_mods,
platform_config_settings = {
Expand All @@ -434,10 +454,9 @@ def _create_unified_hub_repo(mods):

for hub_name in hubs:
# The hubs configured with `pip.configure` only expose some packages.
hub_packages = mods.configured_hubs.get(
hub_name,
mods.hub_whl_map[hub_name],
)
hub_packages = mods.hub_whl_map[hub_name]
if hub_name in mods.configured_hubs:
hub_packages = mods.configured_hubs[hub_name].packages
for pkg_name in hub_packages:
norm_pkg = normalize_name(pkg_name)
if norm_pkg not in packages:
Expand Down Expand Up @@ -469,6 +488,7 @@ def _create_unified_hub_repo(mods):
hubs = hubs,
packages = packages,
requirements_bzl = mods.unified_hub_requirements_bzl,
testonly_packages = mods.unified_testonly_packages,
)

def _pip_impl(module_ctx):
Expand Down Expand Up @@ -562,7 +582,10 @@ def _pip_impl(module_ctx):
groups = mods.hub_group_map.get(hub_name),
# The hubs configured with `pip.configure` hide the other packages.
hide_unexposed_packages = hub_name in mods.configured_hubs,
public_packages = mods.configured_hubs.get(hub_name, []),
public_packages = (
mods.configured_hubs[hub_name].packages if hub_name in mods.configured_hubs else []
),
testonly_packages = mods.testonly_packages.get(hub_name, []),
)

_create_unified_hub_repo(mods)
Expand Down Expand Up @@ -1178,6 +1201,24 @@ the same module and hub, then to the {attr}`pip.parse.pyproject_toml` of the
hub, then to {attr}`pip.default.pyproject_toml` if the hub is defined by the
root module. It is only needed when {attr}`dependency_groups` is set.

:::{versionadded} VERSION_NEXT_FEATURE
:::
""",
),
"testonly": attr.bool(
default = False,
doc = """\
Make the aliases of the selected packages testonly. A package that is also
selected by a `pip.configure` tag without `testonly` is not testonly. It can't
be used with `["*", "transitive"]`, which exposes all of the packages.

The testonly packages are left out of the `all_requirements`,
`all_whl_requirements` and `all_data_requirements` lists in the hub's
`requirements.bzl` and listed in `all_testonly_requirements` instead. In the
[Unified @pypi Hub](unified-pypi-hub), the targets of a package point to the
hub that is selected, so the package is testonly when it is testonly in that
hub.

:::{versionadded} VERSION_NEXT_FEATURE
:::
""",
Expand Down
4 changes: 3 additions & 1 deletion python/private/pypi/generate_whl_library_build_bazel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ _RENDER = {
"requires_dist": render.list,
"srcs_exclude": render.list,
"tags": render.list,
"testonly_deps": str,
}

# NOTE @aignas 2024-10-25: We have to keep this so that files in
Expand Down Expand Up @@ -94,8 +95,9 @@ def generate_whl_library_build_bazel(
# no deps, we can leave the extra loads out
pass
elif config_load:
loads.append("""load("{}", "{}")""".format(config_load, "packages"))
loads.append("""load("{}", "packages", "testonly_packages")""".format(config_load))
kwargs["include"] = "packages"
kwargs["testonly_deps"] = "testonly_packages"
kwargs["requires_dist"] = requires_dist
else:
kwargs["requires_dist"] = requires_dist
Expand Down
28 changes: 22 additions & 6 deletions python/private/pypi/hub_exposure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ def hub_exposure(module_ctx, *, hubs, default_pyproject = None, _fail = fail):
_fail: {type}`callable` the failure function, mainly for testing.

Returns:
{type}`dict[str, list[str]] | None` the configured hubs mapped to the
sorted packages that they expose. The hubs that are not configured or
that expose all of their packages are not included, so that they
behave the same. Returns `None` on failure.
{type}`dict[str, struct] | None` the configured hubs mapped to a
struct with:
* `packages`: {type}`list[str]` the sorted packages that the hub
exposes.
* `testonly_packages`: {type}`list[str]` the sorted packages whose
aliases are testonly. A package is testonly only if all of the
tags that select it are testonly.

The hubs that are not configured or that expose all of their packages
are not included, so that they behave the same. Returns `None` on
failure.
"""

# The root module wins over the owner of the hub when both configure it.
Expand Down Expand Up @@ -60,6 +67,9 @@ def hub_exposure(module_ctx, *, hubs, default_pyproject = None, _fail = fail):
last_pyproject[hub] = tag.pyproject_toml
if not tag.dependency_groups:
# The tag does not select any packages.
if tag.testonly:
_fail("pip.configure() for hub '{}' uses `testonly`, which needs the `dependency_groups` of the testonly packages".format(hub))
return None
continue

pyproject = last_pyproject.get(hub) or info.pyproject
Expand All @@ -80,6 +90,9 @@ def hub_exposure(module_ctx, *, hubs, default_pyproject = None, _fail = fail):
if selection == None:
return None
if selection.all:
if tag.testonly:
_fail("pip.configure() for hub '{}' cannot use `testonly` when exposing all of the packages with {}".format(hub, tag.dependency_groups))
return None
exposes_all[hub] = None

# Multiple tags for the same hub are combined.
Expand All @@ -88,10 +101,13 @@ def hub_exposure(module_ctx, *, hubs, default_pyproject = None, _fail = fail):
# The pyproject.toml may list packages that are not in the hub,
# e.g. tools that are not locked.
if name in info.packages:
exposed[name] = None
exposed[name] = exposed.get(name, True) and tag.testonly

return {
hub: sorted(packages)
hub: struct(
packages = sorted(packages),
testonly_packages = sorted([name for name, testonly in packages.items() if testonly]),
)
for hub, packages in configured.items()
if hub not in exposes_all
}
23 changes: 20 additions & 3 deletions python/private/pypi/hub_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _impl(rctx):
else:
bzl_packages = rctx.attr.packages or rctx.attr.whl_map.keys()
exposed_packages = None
testonly_packages = rctx.attr.testonly_packages
aliases = render_multiplatform_pkg_aliases(
aliases = {
key: _whl_config_settings_from_json(values)
Expand All @@ -41,6 +42,7 @@ def _impl(rctx):
extra_hub_aliases = rctx.attr.extra_hub_aliases,
requirement_cycles = rctx.attr.groups,
platform_config_settings = rctx.attr.platform_config_settings,
testonly_packages = testonly_packages,
)
for path, contents in aliases.items():
rctx.file(path, contents)
Expand All @@ -57,20 +59,31 @@ def _impl(rctx):
rctx.attr._config_template,
substitutions = {
"%%PACKAGES%%": render.dict(rctx.attr.whl_map, value_repr = lambda x: "None"),
"%%TESTONLY_PACKAGES%%": render.list(sorted(testonly_packages)),
},
)

# The testonly packages are only listed in `all_testonly_requirements`,
# which also leaves out the packages that are not available on all of the
# platforms.
all_packages = [p for p in bzl_packages if p not in testonly_packages]
all_testonly_packages = [p for p in bzl_packages if p in testonly_packages]
rctx.template("requirements.bzl", rctx.attr._requirements_bzl_template, substitutions = {
"%%ALL_DATA_REQUIREMENTS%%": render.list([
macro_tmpl.format(p, "data")
for p in bzl_packages
for p in all_packages
]),
"%%ALL_REQUIREMENTS%%": render.list([
macro_tmpl.format(p, "pkg")
for p in bzl_packages
for p in all_packages
]),
"%%ALL_TESTONLY_REQUIREMENTS%%": render.list([
macro_tmpl.format(p, "pkg")
for p in all_testonly_packages
]),
"%%ALL_WHL_REQUIREMENTS_BY_PACKAGE%%": render.dict({
p: macro_tmpl.format(p, "whl")
for p in bzl_packages
for p in all_packages
}),
"%%MACRO_TMPL%%": macro_tmpl,
})
Expand Down Expand Up @@ -114,6 +127,10 @@ the platforms.
mandatory = True,
doc = "The apparent name of the repo. This is needed because in bzlmod, the name attribute becomes the canonical name.",
),
"testonly_packages": attr.string_list(
default = [],
doc = "The packages whose aliases should be testonly.",
),
"whl_map": attr.string_dict(
mandatory = True,
doc = """\
Expand Down
4 changes: 4 additions & 0 deletions python/private/pypi/labels.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ WHEEL_FILE_IMPL_LABEL = "_whl"
WHEEL_FILE = "whl_file"
PY_LIBRARY_PUBLIC_LABEL = "pkg"
PY_LIBRARY_IMPL_LABEL = "_pkg"

# The prefix of the hub targets that the `testonly` public targets wrap. The
# whl repositories depend on them, because they are not `testonly`.
TESTONLY_IMPL_PREFIX = "_testonly_"
PY_SRCS_LABEL = "srcs"
DATA_LABEL = "data"
DIST_INFO_LABEL = "dist_info"
Expand Down
1 change: 1 addition & 0 deletions python/private/pypi/pip_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ def _pip_repository_impl(rctx):
pkg: None
for pkg in bzl_packages
}, value_repr = lambda x: "None"),
"%%TESTONLY_PACKAGES%%": "[]",
},
)
config_repo_template = repr(rctx.attr.name)
Expand Down
Loading