From 85a253d0231272406314bf8c7a77dc8c07dc572c Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:50:18 +0900 Subject: [PATCH 1/3] feat(pypi): add strip_extras to remove the extras from requirement lines --- python/private/pypi/pep508_requirement.bzl | 19 +++++++++++++++++++ tests/pypi/pep508/requirement_tests.bzl | 15 ++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/python/private/pypi/pep508_requirement.bzl b/python/private/pypi/pep508_requirement.bzl index 7552642572..8aaaa29e37 100644 --- a/python/private/pypi/pep508_requirement.bzl +++ b/python/private/pypi/pep508_requirement.bzl @@ -56,3 +56,22 @@ def requirement(spec): extras = extras, version = version, ) + +def strip_extras(requirement_line): + """Remove the extras from the requirement line. + + Args: + requirement_line: {type}`str` the requirement line, e.g. + `foo[bar]==1.0 --hash=sha256:...`. + + Returns: + The requirement line without the extras, e.g. `foo==1.0 --hash=sha256:...`. + """ + for i, c in enumerate(requirement_line.elems()): + if c in "=<>!~;@( ": + break + if c == "[": + _, _, tail = requirement_line[i:].partition("]") + return requirement_line[:i] + tail + + return requirement_line diff --git a/tests/pypi/pep508/requirement_tests.bzl b/tests/pypi/pep508/requirement_tests.bzl index 2ce45922da..c027209aa1 100644 --- a/tests/pypi/pep508/requirement_tests.bzl +++ b/tests/pypi/pep508/requirement_tests.bzl @@ -14,7 +14,7 @@ """Tests for parsing the requirement specifier.""" load("@rules_testing//lib:test_suite.bzl", "test_suite") -load("//python/private/pypi:pep508_requirement.bzl", "requirement") # buildifier: disable=bzl-visibility +load("//python/private/pypi:pep508_requirement.bzl", "requirement", "strip_extras") # buildifier: disable=bzl-visibility _tests = [] @@ -42,6 +42,19 @@ def _test_requirement_line_parsing(env): _tests.append(_test_requirement_line_parsing) +def _test_strip_extras(env): + for line, want in { + "foo": "foo", + "foo @ https://example.org/foo[1].whl": "foo @ https://example.org/foo[1].whl", + "foo==1.0 ; python_version < '3.12' --hash=sha256:deadbeef": "foo==1.0 ; python_version < '3.12' --hash=sha256:deadbeef", + "foo[bar,baz] @ https://example.org/foo.whl": "foo @ https://example.org/foo.whl", + "foo[bar]": "foo", + "foo[bar]==1.0 --hash=sha256:deadbeef": "foo==1.0 --hash=sha256:deadbeef", + }.items(): + env.expect.where(line = line).that_str(strip_extras(line)).equals(want) + +_tests.append(_test_strip_extras) + def requirement_test_suite(name): # buildifier: disable=function-docstring test_suite( name = name, From 038f9b109a044f303d35e4e70da39dd2414d0aa3 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:23:24 +0900 Subject: [PATCH 2/3] feat(pypi): add keep_extras and dependency excludes to pep508_deps --- python/private/pypi/pep508_deps.bzl | 31 ++++++++++++++---------- tests/pypi/pep508/deps_tests.bzl | 37 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/python/private/pypi/pep508_deps.bzl b/python/private/pypi/pep508_deps.bzl index e36ceadbd5..da42fbe5e9 100644 --- a/python/private/pypi/pep508_deps.bzl +++ b/python/private/pypi/pep508_deps.bzl @@ -26,23 +26,32 @@ def deps( requires_dist, extras = [], excludes = [], - include = []): + include = [], + keep_extras = False): """Parse the RequiresDist from wheel METADATA Args: name: {type}`str` the name of the wheel. requires_dist: {type}`list[str]` the list of RequiresDist lines from the METADATA file. - excludes: {type}`list[str]` what packages should we exclude. + excludes: {type}`list[str]` what dependencies should we exclude, e.g. + `foo` or `foo[bar]`, in the same format as the returned values. include: {type}`list[str]` what packages should we exclude. If it is not specified, then we will include all deps from `requires_dist`. extras: {type}`list[str]` the requested extras to generate targets for. + keep_extras: {type}`bool` keep the extras of the dependencies, if + `True`, the dependencies with extras are returned as `foo[bar]`. Returns: A struct with attributes: * deps: {type}`list[str]` dependencies to include unconditionally. * deps_select: {type}`dict[str, list[str]]` dependencies to include on particular subset of target platforms. + + The dependencies are normalized names, e.g. `foo`, or normalized names + with a single extra, e.g. `foo[bar]`, if the dependency is requested + with extras and `keep_extras` is set. A dependency with multiple + extras, e.g. `foo[bar,baz]`, is returned as `foo[bar]` and `foo[baz]`. """ if not requires_dist: return struct( @@ -60,25 +69,25 @@ def deps( want_extras = _resolve_extras(name, reqs, extras) include = [normalize_name(n) for n in include] - # drop self edges - excludes = [name] + [normalize_name(x) for x in excludes] - - reqs_by_name = {} + reqs_by_dep = {} for req in reqs: - if req.name_ in excludes: + # drop self edges + if req.name_ == name: continue if include and req.name_ not in include: continue - reqs_by_name.setdefault(req.name, []).append(req) + for dep in ["{}[{}]".format(req.name_, e) for e in req.extras if keep_extras] or [req.name_]: + if dep not in excludes: + reqs_by_dep.setdefault(dep, []).append(req) - for name, reqs in reqs_by_name.items(): + for dep, reqs in reqs_by_dep.items(): _add_reqs( deps, deps_select, - normalize_name(name), + dep, reqs, extras = want_extras, ) @@ -92,8 +101,6 @@ def deps( ) def _add(deps, deps_select, dep, markers = None): - dep = normalize_name(dep) - if not markers: deps[dep] = True elif len(markers) == 1: diff --git a/tests/pypi/pep508/deps_tests.bzl b/tests/pypi/pep508/deps_tests.bzl index 0d171d8e95..b7a58a45a0 100644 --- a/tests/pypi/pep508/deps_tests.bzl +++ b/tests/pypi/pep508/deps_tests.bzl @@ -320,6 +320,43 @@ def test_extras_with_hyphens_are_normalized(env): _tests.append(test_extras_with_hyphens_are_normalized) +def test_deps_with_extras(env): + got = deps( + "foo", + requires_dist = [ + "bar[Socks]", + "baz[a,b]; sys_platform == 'linux'", + "qux", + "qux[c]; extra == 'feat'", + ], + extras = ["feat"], + keep_extras = True, + ) + + # The extras of the dependencies are kept so that the targets with the + # extras can be used. + env.expect.that_collection(got.deps).contains_exactly(["bar[socks]", "qux", "qux[c]"]) + env.expect.that_dict(got.deps_select).contains_exactly({ + "baz[a]": "sys_platform == \"linux\"", + "baz[b]": "sys_platform == \"linux\"", + }) + +_tests.append(test_deps_with_extras) + +def test_deps_with_extras_excludes_and_includes(env): + got = deps( + "foo", + requires_dist = ["bar", "bar[socks]", "baz[a]", "baz[b]", "qux[b]"], + # The excludes are dependencies and the includes are packages + excludes = ["bar", "baz[a]"], + include = ["bar", "baz"], + keep_extras = True, + ) + + env.expect.that_collection(got.deps).contains_exactly(["bar[socks]", "baz[b]"]) + +_tests.append(test_deps_with_extras_excludes_and_includes) + def deps_test_suite(name): # buildifier: disable=function-docstring test_suite( name = name, From 1a65aeda692e3e7689faca1364fcbafe48a0cde6 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:51:20 +0900 Subject: [PATCH 3/3] feat(pypi): add extra_label and group_member label helpers --- python/private/pypi/BUILD.bazel | 11 ++++++----- python/private/pypi/labels.bzl | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel index 03a9e113d5..8f8c249463 100644 --- a/python/private/pypi/BUILD.bazel +++ b/python/private/pypi/BUILD.bazel @@ -629,6 +629,12 @@ bzl_library( ], ) +bzl_library( + name = "labels", + srcs = ["labels.bzl"], + deps = ["//python/private:normalize_name"], +) + bzl_library( name = "argparse", srcs = ["argparse.bzl"], @@ -654,11 +660,6 @@ bzl_library( srcs = ["hash.bzl"], ) -bzl_library( - name = "labels", - srcs = ["labels.bzl"], -) - bzl_library( name = "package_annotation", srcs = ["package_annotation.bzl"], diff --git a/python/private/pypi/labels.bzl b/python/private/pypi/labels.bzl index c77167b593..f6257106e9 100644 --- a/python/private/pypi/labels.bzl +++ b/python/private/pypi/labels.bzl @@ -14,6 +14,8 @@ """Constants used by parts of pip_repository for naming libraries and wheels.""" +load("//python/private:normalize_name.bzl", "normalize_name") + EXTRACTED_WHEEL_FILES = "extracted_whl_files" WHEEL_FILE_PUBLIC_LABEL = "whl" WHEEL_FILE_IMPL_LABEL = "_whl" @@ -24,3 +26,30 @@ PY_SRCS_LABEL = "srcs" DATA_LABEL = "data" DIST_INFO_LABEL = "dist_info" NODEPS_LABEL = "no_deps" + +def extra_label(extra): + """Return the label name for the target that includes an extra. + + Args: + extra: {type}`str` the normalized name of the extra, an empty string + means the base package without any extras. + + Returns: + The target name, e.g. `[extra]`. + """ + return "[{}]".format(extra) + +def group_member(spec): + """Normalize a member of a dependency group. + + Args: + spec: {type}`str` the package name, e.g. `Foo`, or the package name + with a single extra, e.g. `Foo[Bar]`. + + Returns: + The normalized member, e.g. `foo` or `foo[bar]`. + """ + name, _, extra = spec.partition("[") + name = normalize_name(name.strip()) + extra = normalize_name(extra.partition("]")[0].strip()) if extra else "" + return "{}[{}]".format(name, extra) if extra else name