Skip to content
Draft
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 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
96 changes: 93 additions & 3 deletions docs/pypi/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,101 @@ 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, depend on the target of each extra:

```starlark
py_library(
name = "my_lib",
deps = [
":other_lib",
"@pypi//google_api_core:[async_rest]",
"@pypi//google_api_core:[grpc]",
"@pypi//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` depends on
`@pypi//bar:[socks]`. If the extra is not in the lock file, then building
`@pypi//foo` fails with a message asking you to add `bar[socks]` to the
requirements or the lock file. For example, when compiling the requirements
with `uv pip compile`, pass `--no-strip-extras` to keep the extras in the
output.

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 dependencies
between the packages use the `pkg` targets and ignore the extras from the wheel
`METADATA` like before, so the extras need to be in the lock file.

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
16 changes: 14 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") # buildifier: disable=bzl-visibility

all_requirements = [
"@my_project_pip_deps_vendored_certifi//:pkg",
Expand Down Expand Up @@ -79,7 +80,16 @@ _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]`.

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

def whl_requirement(name):
return "@my_project_pip_deps_vendored_{}//:{}".format(pip_utils.normalize_name(name), "whl")
Expand Down Expand Up @@ -107,8 +117,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
15 changes: 15 additions & 0 deletions news/2786.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(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]")`. 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)).
17 changes: 17 additions & 0 deletions news/2786.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(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`
depends on `@pypi//bar:[socks]` instead of `@pypi//bar`. Previously such extras
were ignored. If the lock file of a hub does not request the extra, the build
fails with a message asking to add it, e.g. by compiling the requirements with
`--no-strip-extras`. This does not apply when there is no hub, e.g. with a
vendored `requirements.bzl`, where the dependencies use the `pkg` targets,
which include the extras from the lock file, as before. 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)).
30 changes: 21 additions & 9 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,23 +104,42 @@ 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",
# 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",
# Without a hub the dependencies use the `pkg` target, which includes
# the extras of the requirement.
requirement = "pkg[optional]",
# 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}",
)
_whl_library_from_dir(
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 +153,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