Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 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
0abc939
refactor(pypi): support venvs in the unified hub
aignas Nov 7, 2026
c96fada
feat(pypi): add pip.configure.venv to create named package selections
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
3bc532c
Merge branch 'aignas.feat.pip-configure-testonly' into aignas.feat.pi…
aignas Nov 8, 2026
5a97b92
refactor(pypi): map the venvs to their hubs without config_setting gr…
aignas Nov 8, 2026
7db8744
refactor(pypi): compute the hubs used by venvs once
aignas Nov 8, 2026
71be0c0
fix(pypi): only pass testonly to the package alias when it is set
aignas Nov 8, 2026
6716cc0
Merge branch 'aignas.feat.pip-configure-testonly' into aignas.feat.pi…
aignas Nov 8, 2026
8cb7256
test(pypi): pass the pyproject.toml in the venv extension test
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
e4b6f48
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
15a8480
Merge branch 'aignas.feat.pip-configure-testonly' into aignas.feat.pi…
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
4a38170
Merge remote-tracking branch 'origin/aignas.feat.pip-configure-teston…
aignas Nov 19, 2026
18258a4
fix(pypi): let the packages depend on the testonly packages of the hub
aignas Nov 22, 2026
08d5383
test(pypi): select the dependency groups of the dev venv in the integ…
aignas Nov 22, 2026
290cf5a
Merge branch 'aignas.feat.pip-configure-testonly' into aignas.feat.pi…
aignas Nov 22, 2026
8ba32e4
Merge remote-tracking branch 'upstream/main' into aignas.feat.pip-con…
aignas Nov 22, 2026
16a4fbe
Merge branch 'aignas.feat.pip-configure-testonly' into aignas.feat.pi…
aignas Nov 22, 2026
c3f567b
fix(pypi): fail on pip.configure.venv when a hub is named pypi
aignas Nov 22, 2026
fe45b4e
fix(pypi): substitute the testonly packages in the WORKSPACE config repo
aignas Nov 22, 2026
0f2d36b
Merge branch 'aignas.feat.pip-configure-testonly' into aignas.feat.pi…
aignas Nov 22, 2026
bc5c3a5
fix(pypi): only mark the packages as testonly in the unified hub if a…
aignas Nov 23, 2026
7f833e6
Merge branch 'aignas.feat.pip-configure-testonly' into aignas.feat.pi…
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
53 changes: 53 additions & 0 deletions docs/pypi/download.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,59 @@ 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.

#### Venvs

Set {attr}`pip.configure.venv` to create a venv, a named selection of the
packages of a hub, instead of changing the hub:

```starlark
pip.configure(
hub_name = "pypi_hub",
venv = "pypi_hub_docs",
dependency_groups = ["default", "group:docs"],
)
```

The venv is used through the [Unified @pypi Hub](unified-pypi-hub) by setting
`--@rules_python//python/config_settings:venv=pypi_hub_docs`. It resolves the
`@pypi//<pkg>` labels to the packages of `pypi_hub`, so nothing is downloaded
twice, and the packages that are not selected are reported as missing. A venv
does not create a new repository and does not change the hub.

A venv name must not collide with a hub and a venv can't be used with two
different hubs. The packages of a venv are testonly if they are testonly in
the hub. The venvs can't be used when a hub is named `pypi`, because such a hub
replaces the unified hub.

:::{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)).
4 changes: 4 additions & 0 deletions news/4429.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(pypi) Added {attr}`pip.configure.venv` to create a named selection of the
packages of a hub that can be used through the unified `@pypi` hub with the
{flag}`--venv` flag
([#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%%
108 changes: 96 additions & 12 deletions python/private/pypi/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -378,29 +378,51 @@ You cannot use both the additive_build_content and additive_build_content_file a
packages = out.whl_map.keys(),
)

configured_hubs = hub_exposure(
configured = hub_exposure(
module_ctx,
hubs = hubs,
default_pyproject = config.pyproject_toml,
_fail = _fail,
)
if configured_hubs == None:
if configured == None:
return None

# 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}
configured_hubs = {}
venvs = {}
testonly_packages = {}
unified_testonly_packages = {}
for hub_name, info in configured.items():
if hub_name != info.hub:
venvs[hub_name] = info
continue

configured_hubs[hub_name] = info
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,
venvs = venvs,
config = config,
declared_deps = declared_deps,
default_hub = config.default_hub or renamed_default_hub,
Expand All @@ -409,7 +431,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 @@ -432,20 +460,25 @@ def _create_unified_hub_repo(mods):
packages = {}
extra_aliases = {}

for hub_name in hubs:
# The venvs are named selections of the packages of a hub, which can be
# selected with the same `venv` flag as the hubs.
venvs = {name: info.hub for name, info in mods.venvs.items()}
for hub_name in hubs + sorted(venvs):
# The hubs configured with `pip.configure` only expose some packages.
hub_packages = mods.configured_hubs.get(
hub_name,
mods.hub_whl_map[hub_name],
)
if hub_name in venvs:
hub_packages = mods.venvs[hub_name].packages
elif hub_name in mods.configured_hubs:
hub_packages = mods.configured_hubs[hub_name].packages
else:
hub_packages = mods.hub_whl_map[hub_name]
for pkg_name in hub_packages:
norm_pkg = normalize_name(pkg_name)
if norm_pkg not in packages:
packages[norm_pkg] = []
if hub_name not in packages[norm_pkg]:
packages[norm_pkg].append(hub_name)

extra = mods.extra_aliases.get(hub_name, {}).get(norm_pkg, [])
extra = mods.extra_aliases.get(venvs.get(hub_name, hub_name), {}).get(norm_pkg, [])
for alias_name in extra:
qual_alias = "%s:%s" % (norm_pkg, alias_name)
if qual_alias not in extra_aliases:
Expand All @@ -469,6 +502,8 @@ def _create_unified_hub_repo(mods):
hubs = hubs,
packages = packages,
requirements_bzl = mods.unified_hub_requirements_bzl,
venvs = venvs,
testonly_packages = mods.unified_testonly_packages,
)

def _pip_impl(module_ctx):
Expand Down Expand Up @@ -548,7 +583,12 @@ def _pip_impl(module_ctx):
for name, args in mods.whl_libraries.items():
whl_library(name = name, **args)

# The venvs may select the packages that their hub does not expose.
venv_hubs = {info.hub: None for info in mods.venvs.values()}
for hub_name, whl_map in mods.hub_whl_map.items():
extra_internal_visibility = []
if hub_name in venv_hubs:
extra_internal_visibility = ["@pypi//:__subpackages__"]
hub_repository(
name = hub_name,
repo_name = hub_name,
Expand All @@ -562,7 +602,11 @@ 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, []),
extra_internal_visibility = extra_internal_visibility,
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 +1222,46 @@ 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
:::
""",
),
"venv": attr.string(
mandatory = False,
doc = """\
Create a venv instead of configuring the hub. Defaults to {attr}`hub_name`.

A venv is a named selection of the packages of the hub that is used through
the [Unified @pypi Hub](unified-pypi-hub) by setting
{flag}`--@rules_python//python/config_settings:venv` to its name. It does not
create a new repository and the hub itself is not changed. The packages that
are not selected are reported as missing, like the packages that are not in a
hub.

The name must not collide with a hub and a venv can't be used with two
different hubs. Multiple tags with the same `venv` are combined and each of
them needs the {attr}`dependency_groups` of its packages. `testonly` is not
supported for venvs. The venvs can't be used when a hub is named `pypi`,
because such a hub replaces the unified 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
Loading