Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
2873e71
feat(pypi): add strip_extras to remove the extras from requirement lines
aignas Nov 13, 2026
2f92d4e
feat(pypi): add keep_extras and dependency excludes to pep508_deps
aignas Nov 13, 2026
efc64f0
feat(pypi): add extra_label and group_member label helpers
aignas Nov 13, 2026
552d8f1
feat(pypi): create a target for every provided extra in the whl repos…
aignas Nov 13, 2026
ace0065
feat(pypi): record the requested extras in whl_config_setting
aignas Nov 13, 2026
c0bd9cc
feat(pypi): add the per-platform extra targets to the hub packages
aignas Nov 13, 2026
998146c
feat(pypi): use a single whl repository for the different extras of a…
aignas Nov 13, 2026
dfaa556
feat(pypi): support extras in the requirement macro and add the requi…
aignas Nov 13, 2026
6e77448
refactor(pypi): extract the failing action of missing_package_error
aignas Nov 13, 2026
3284430
feat(pypi): fail the build when a required extra of a dependency is m…
aignas Nov 13, 2026
cca8d1b
feat(pypi): support the extra targets in WORKSPACE
aignas Nov 13, 2026
181ddc9
feat(pypi): support extras as members of the requirement cycle groups
aignas Nov 13, 2026
cf3b4b6
refactor(pypi): pass the requirement line only to the repositories us…
aignas Nov 13, 2026
be422b2
test(pypi): check the extras of the dependencies from the METADATA
aignas Nov 13, 2026
646f270
docs(pypi): document the extra targets
aignas Nov 13, 2026
dafa838
chore(examples): update the vendored requirements.bzl
aignas Nov 13, 2026
6431f8b
fix(pypi): parse the whl METADATA with CRLF line endings
aignas Nov 13, 2026
c2e07df
test(pypi): drop the requirement from the whl repositories in the int…
aignas Nov 13, 2026
9f9fc9f
refactor(pypi): keep the original hub_builder group map and repo mapp…
aignas Nov 13, 2026
a24fac6
refactor(pypi): minimize the changes to the group library generation
aignas Nov 13, 2026
8f57a4f
test(pypi): remove the overlapping extras tests
aignas Nov 13, 2026
c488c62
Merge remote-tracking branch 'upstream/main' into feat.aignas.target-…
aignas Nov 15, 2026
65a8f92
Merge remote-tracking branch 'upstream/main' into feat.aignas.target-…
aignas Nov 17, 2026
c54d57d
feat(pypi): add rules to depend on an extra of a package by its name
aignas Nov 17, 2026
a094169
feat(pypi): wire the extras of the dependencies without the hub
aignas Nov 17, 2026
a055395
refactor(pypi): stop passing the available extras to the whl reposito…
aignas Nov 17, 2026
45555f3
docs(pypi): document that the extras of the dependencies need not be …
aignas Nov 17, 2026
b29c3c6
feat(pypi): add targets for the extras of the packages
aignas Nov 22, 2026
f210efe
Merge remote-tracking branch 'upstream/main' into feat.aignas.target-…
aignas Nov 22, 2026
33f8bea
fix(pypi): normalize the excludes of the pep508 deps
aignas Nov 22, 2026
dfb6fe7
fix(pypi): resolve the self-referencing extras of multi-word packages
aignas Nov 22, 2026
6a76cde
fix(pypi): use the extras of the requirement in the whl repos
aignas Nov 22, 2026
ebde6bf
feat(pypi): add the targets for the extras to the unified hub
aignas Nov 22, 2026
7b8c8ba
fix(pypi): fall back to the package without extras for unknown extras
aignas Nov 22, 2026
1c06620
docs(pypi): describe the behaviour changes of the extras targets
aignas Nov 22, 2026
f9c8777
Merge remote-tracking branch 'upstream/main' into feat.aignas.extras-…
aignas Nov 22, 2026
09397b5
Merge remote-tracking branch 'origin/feat.aignas.target-per-extra' in…
aignas Nov 22, 2026
4ae37bd
fix(pypi): forward all providers and propagate aspects in extra_dep
aignas Nov 22, 2026
4fb97d1
feat(pypi): resolve the extras of the dependencies in the whl repos
aignas Nov 22, 2026
4c4672d
refactor(pypi): remove the unused missing_extra_error rule
aignas Nov 22, 2026
d611f6e
docs(pypi): document that the extras of the dependencies need not be …
aignas Nov 22, 2026
d5c7eba
feat(pypi): use the extras in the markers as the extras that a packag…
aignas Nov 22, 2026
47c96e0
Merge branch 'feat.aignas.target-per-extra' into feat.aignas.extras-w…
aignas Nov 22, 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
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ use_repo(
"pkgutil_nspkg2",
"rules_python_runtime_env_tc_info",
"somepkg_with_build_files",
"whl_library_extras_consumer",
"whl_library_extras_direct_dep",
"whl_with_build_files",
"whl_with_data1",
Expand Down
26 changes: 26 additions & 0 deletions docs/pypi/circular-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ be a part of the `airflow` cycle. For instance:
)
```

Each extra of a package has its own target, so a cycle that goes through an
extra back to the package without extras, e.g. `foo[bar]` depending on `baz`
which depends on `foo`, is not a cycle in Bazel and does not need a group. If
the cycle includes the extra itself, e.g. `baz` depends on `foo[bar]`, then
only the extra needs to be a member of the group:

```starlark
...
experimental_requirement_cycles = {
"foo": [
"foo[bar]",
"baz",
]
}
)
```

Only the targets of the members, `@pypi//foo:[bar]` and `@pypi//baz`, are
then part of the group and the other targets of `foo`, e.g. `@pypi//foo:[]`,
can be used without the group. The `whl` targets do not use the extras, so
only the packages without extras are part of the group for the `whl` targets.

:::{versionadded} VERSION_NEXT_FEATURE
Support for the extras as members of the groups.
:::

Alternatively, one could resolve the cycle by removing one leg of it.

For example, while `apache-airflow-providers-sqlite` is "baked into" the Airflow
Expand Down
103 changes: 100 additions & 3 deletions docs/pypi/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,108 @@ which can help you create a `py_binary` target for a particular console script e

[whl_ep]: https://packaging.python.org/specifications/entry-points/

{#extras}
## 'Extras' dependencies

Any "extras" specified in the requirements lock file will be automatically added
as transitive dependencies of the package. In the example above, you'd just put
`requirement("useful_dep")` or `@pypi//useful_dep`.
Any "extras" specified in the requirements lock file or the `uv.lock` file will
be automatically added as transitive dependencies of the package. In the
example above, you'd just put `requirement("useful_dep")` or
`@pypi//useful_dep`.

If you need only some of the extras, the hub repo also has a target for the
package without any extras and a target for each extra used in the lock file.
For example, if the lock file has `google-api-core[async_rest,grpc]`, then the
hub has:

```
@pypi//google_api_core # The package with the requested extras
@pypi//google_api_core:[] # The package without any extras
@pypi//google_api_core:[async_rest] # The package with the async_rest extra
@pypi//google_api_core:[grpc] # The package with the grpc extra
```

Extra names are normalized in the same way as the package names, so
`async-rest` becomes `async_rest`.

The `requirement` helper supports the same syntax:

```starlark
load("@pypi//:requirements.bzl", "requirement")

py_library(
name = "my_lib",
deps = [
requirement("google-api-core[grpc]"),
],
)
```

There are no targets for combinations of extras, because each `[extra]` target
already includes the package. The `requirement` helper accepts at most one
extra. To request more than one extra, use the `requirements` helper, which
accepts any number of packages and returns a list with a label for each
package and each extra:

```starlark
load("@pypi//:requirements.bzl", "requirements")

py_library(
name = "my_lib",
deps = [
":other_lib",
] + requirements(
"google-api-core[async_rest,grpc]",
"numpy",
),
)
```

If the lock file requests different extras for a package on different
platforms, for example `foo[bar]` on Linux and `foo[baz]` on macOS, then
`@pypi//foo` includes only the extras requested for the target platform and
`@pypi//foo:[bar]` on macOS is the same as `@pypi//foo:[]`.

The dependencies of the packages use the extras written in the wheel
`METADATA`, so if `foo` depends on `bar[socks]`, then `@pypi//foo` includes
`bar` and the dependencies of its `socks` extra. The extras of the
dependencies are resolved by the repository of `bar` from its own `METADATA`,
so they do not need to be in the lock file, e.g. when the requirements are
compiled with the extras stripped. This does not change `@pypi//bar`, which
only includes the extras requested in the lock file. The dependencies of the
extra that are not in the lock file are left out like any other dependency
that is not in the lock file. If `bar` does not provide the `socks` extra,
then only `bar` is used and a warning is printed when `@pypi//foo` is
analyzed, e.g. `WARNING: bar 1.0 does not provide the extra 'socks'`, which is
how `pip` handles the unknown extras.

If the lock file requests an extra that the package does not provide, e.g.
`foo[nope]`, then `@pypi//foo:[nope]` is the same as `@pypi//foo:[]`, because
an unknown extra does not have any dependencies. A warning is printed when the
package is loaded, e.g. `WARNING: foo 1.0 does not provide the extra 'nope'`,
which is how `pip` handles the unknown extras. The extras that a package
provides are the `Provides-Extra` fields of the wheel `METADATA` and the extras
that are used in the markers of its requirements, e.g. `extra == "socks"`,
because the old metadata versions do not have the `Provides-Extra` fields.

In bzlmod the extras are not passed to the repositories of the wheels, so
changing the extras in the lock file does not refetch them.

When the `requirements.bzl` file is vendored, there is no hub repo and the
labels returned by the `requirement` helper point to the repositories of the
wheels. Their `pkg` targets include the extras from the lock file and they
have the `[]` target and the `[extra]` targets for the extras used in the lock
file and for all of the extras that the package provides.

The `whl` targets, e.g. `@pypi//foo:whl`, do not use the extras and include
only the wheels of the package and its dependencies without extras.

The packages in a dependency group can also have only some of their extras in
the group, see the [circular dependencies](./circular-dependencies) docs.

:::{versionadded} VERSION_NEXT_FEATURE
The `[]` and `[extra]` targets, the support for extras in the `requirement`
helper and the `requirements` helper were added.
:::

## Consuming Wheel Dists Directly

Expand Down
28 changes: 26 additions & 2 deletions examples/pip_parse_vendored/requirements.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

load("@rules_python//python:pip.bzl", "pip_utils")
load("@rules_python//python/pip_install:pip_repository.bzl", "whl_config_repo", "whl_library")
load("@rules_python//python/private/pypi:labels.bzl", "requirement_label", "requirement_labels") # buildifier: disable=bzl-visibility

all_requirements = [
"@my_project_pip_deps_vendored_certifi//:pkg",
Expand Down Expand Up @@ -79,7 +80,28 @@ _config = {
_annotations = {}

def requirement(name):
return "@my_project_pip_deps_vendored_{}//:{}".format(pip_utils.normalize_name(name), "pkg")
"""Return the label for the package.

Args:
name: The name of the package with at most one extra, e.g. `foo`,
`foo[]` or `foo[bar]`. Use `requirements` for multiple extras.

Returns:
The label of the package.
"""
return requirement_label("@my_project_pip_deps_vendored_{}//:{}", name)

def requirements(*names):
"""Return the labels for the packages.

Args:
*names: The names of the packages with optional extras, e.g. `foo`,
`foo[]`, `foo[bar]` or `foo[bar,baz]`.

Returns:
The list of labels, with a label for each extra of a package.
"""
return requirement_labels("@my_project_pip_deps_vendored_{}//:{}", *names)

def whl_requirement(name):
return "@my_project_pip_deps_vendored_{}//:{}".format(pip_utils.normalize_name(name), "whl")
Expand Down Expand Up @@ -107,8 +129,10 @@ def install_deps(**whl_library_kwargs):
# Set up the requirement groups
all_requirement_groups = {}

# The members are the packages, e.g. `foo`, or the extras of the packages,
# e.g. `foo[bar]`.
requirement_group_mapping = {
requirement: group_name
requirement.partition("[")[0]: group_name
for group_name, group_requirements in all_requirement_groups.items()
for requirement in group_requirements
}
Expand Down
17 changes: 17 additions & 0 deletions news/2786.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(pypi) The hub repositories now have a `[]` target for each package without
any extras and an `[extra]` target for each extra used in the lock file, e.g.
`@pypi//google_api_core:[grpc]`, in both bzlmod and `WORKSPACE`. The unified
`@pypi` hub of bzlmod has the same targets. The `requirement` macro accepts a
single extra, e.g. `requirement("google-api-core[grpc]")`, and the new
`requirements` macro returns a list of labels for any number of packages and
extras, e.g. `requirements("google-api-core[async_rest,grpc]", "numpy")`. The
whl repositories also have the `[]` target and an `[extra]` target for every
extra that the package provides or the lock file requests, so they can be used
with a vendored `requirements.bzl`. In bzlmod the requested extras are no
longer passed to the whl repositories, so changing them does not refetch the
repositories. The members of
`experimental_requirement_cycles` can now be the extras of the packages, e.g.
`foo[bar]`. See
[the docs](https://rules-python.readthedocs.io/en/latest/pypi/use.html#extras)
([#2786](https://github.com/bazel-contrib/rules_python/issues/2786),
[#4168](https://github.com/bazel-contrib/rules_python/issues/4168)).
16 changes: 16 additions & 0 deletions news/2786.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(pypi) The dependencies between the PyPI packages now use the extras from the
wheel `METADATA`, e.g. if `foo` requires `bar[socks]`, then `@pypi//foo`
includes `bar` and the dependencies of its `socks` extra. Previously such
extras were ignored. The extra does not need to be requested in the lock file
of the hub, because it is resolved by the whl repository of `bar`, and the
dependencies of the extra that are not in the lock file are left out. The
`whl` targets no longer include the wheels of the dependencies of the extras.
A member of `experimental_requirement_cycles` without extras, e.g. `foo`, is
now only the package without extras, so the cycles that go through `foo[bar]`
need `foo[bar]` as a member. The same distribution that is requested with
different extras on different platforms now uses a single whl repository. The
`@pypi//foo` targets still include the extras requested in the lock file. An
extra that the package does not provide is still ignored, but a warning is now
printed like `pip` does, e.g.
`WARNING: foo 1.0 does not provide the extra 'nope'`
([#2786](https://github.com/bazel-contrib/rules_python/issues/2786)).
29 changes: 19 additions & 10 deletions python/private/internal_dev_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def _internal_dev_deps_impl(mctx):
whl_library(
name = "somepkg_with_build_files",
whl_file = "@whl_with_build_files//:somepkg-1.0-any-none-any.whl",
requirement = "somepkg",
)

# Setup for //tests/implicit_namespace_packages
Expand All @@ -51,7 +50,6 @@ def _internal_dev_deps_impl(mctx):
whl_library(
name = "implicit_namespace_ns_sub1",
whl_file = "@implicit_namespace_ns_sub1_whl//:ns_sub1-1.0-any-none-any.whl",
requirement = "ns-sub1",
enable_implicit_namespace_pkgs = False,
)

Expand All @@ -63,7 +61,6 @@ def _internal_dev_deps_impl(mctx):
whl_library(
name = "implicit_namespace_ns_sub2",
whl_file = "@implicit_namespace_ns_sub2_whl//:ns_sub2-1.0-any-none-any.whl",
requirement = "ns-sub2",
enable_implicit_namespace_pkgs = False,
)

Expand All @@ -75,7 +72,6 @@ def _internal_dev_deps_impl(mctx):
whl_library(
name = "pkgutil_nspkg1",
whl_file = "@pkgutil_nspkg1_whl//:pkgutil_nspkg1-1.0-any-none-any.whl",
requirement = "pkgutil_nspkg1",
enable_implicit_namespace_pkgs = False,
)

Expand All @@ -87,7 +83,6 @@ def _internal_dev_deps_impl(mctx):
whl_library(
name = "pkgutil_nspkg2",
whl_file = "@pkgutil_nspkg2_whl//:pkgutil_nspkg2-1.0-any-none-any.whl",
requirement = "pkgutil_nspkg2",
enable_implicit_namespace_pkgs = False,
)

Expand All @@ -99,7 +94,6 @@ def _internal_dev_deps_impl(mctx):
whl_library(
name = "whl_with_data1",
whl_file = "@whl_with_data1_whl//:whl_with_data1-1.0-any-none-any.whl",
requirement = "whl-with-data1",
)

whl_from_dir_repo(
Expand All @@ -110,14 +104,31 @@ def _internal_dev_deps_impl(mctx):
whl_library(
name = "whl_with_data2",
whl_file = "@whl_with_data2_whl//:whl_with_data2-1.0-any-none-any.whl",
requirement = "whl-with-data2",
)

_whl_library_from_dir(
name = "whl_library_extras_direct_dep",
root = "//tests/pypi/whl_library/testdata/pkg:BUILD.bazel",
output = "pkg-1.0-any-none-any.whl",
requirement = "pkg[optional]",
# The `pkg` target includes the requested extras and the package does
# not provide the `unknown` extra.
requirement = "pkg[optional,unknown]",
# The following is necessary to enable pipstar and make tests faster
config_load = "@rules_python//tests/pypi/whl_library/testdata:packages.bzl",
dep_template = "@whl_library_extras_{name}//:{target}",
)
_whl_library_from_dir(
name = "whl_library_extras_pkg",
root = "//tests/pypi/whl_library/testdata/pkg:BUILD.bazel",
output = "pkg-1.0-any-none-any.whl",
# The following is necessary to enable pipstar and make tests faster
config_load = "@rules_python//tests/pypi/whl_library/testdata:packages.bzl",
dep_template = "@whl_library_extras_{name}//:{target}",
)
_whl_library_from_dir(
name = "whl_library_extras_consumer",
root = "//tests/pypi/whl_library/testdata/consumer:BUILD.bazel",
output = "consumer-1.0-any-none-any.whl",
# The following is necessary to enable pipstar and make tests faster
config_load = "@rules_python//tests/pypi/whl_library/testdata:packages.bzl",
dep_template = "@whl_library_extras_{name}//:{target}",
Expand All @@ -126,7 +137,6 @@ def _internal_dev_deps_impl(mctx):
name = "whl_library_extras_optional_dep",
root = "//tests/pypi/whl_library/testdata/optional_dep:BUILD.bazel",
output = "optional_dep-1.0-any-none-any.whl",
requirement = "optional_dep",
# The following is necessary to enable pipstar and make tests faster
config_load = "@rules_python//tests/pypi/whl_library/testdata:packages.bzl",
)
Expand All @@ -140,7 +150,6 @@ def _internal_dev_deps_impl(mctx):
whl_library(
name = "patch_whl_pkg",
whl_file = "@patch_whl_pkg_whl//:pkg-1.0-any-none-any.whl",
requirement = "pkg",
whl_patches = {
"//tests/pypi/patch_whl/testdata/patches:modify_pkg.patch": json.encode({
"patch_strip": 0,
Expand Down
Loading
Loading