feat(pypi): use the none-any wheel as a fallback with target_platforms = ["*"] When `target_platforms = ["*"]` is used with a `uv.lock` file, add the platform independent wheel of a package, if there is one, as a fallback for the Python version. The platforms that are not configured with `pip.default` can then still use the package, whilst the configured platforms keep using the more specific wheels.
diff --git a/news/pip-target-platforms-all.added.md b/news/pip-target-platforms-all.added.md index cb690ce..374a45f 100644 --- a/news/pip-target-platforms-all.added.md +++ b/news/pip-target-platforms-all.added.md
@@ -1,2 +1,4 @@ (pypi) {attr}`pip.parse.target_platforms` accepts `"*"` to evaluate the -requirements for all of the platforms configured with {obj}`pip.default`. +requirements for all of the platforms configured with {obj}`pip.default`. With +{attr}`pip.parse.uv_lock`, the `none-any` wheel of a package is also used for +the platforms that are not configured.
diff --git a/python/private/pypi/extension.bzl b/python/private/pypi/extension.bzl index 908a40b..fa328d6 100644 --- a/python/private/pypi/extension.bzl +++ b/python/private/pypi/extension.bzl
@@ -907,7 +907,9 @@ a string `"{os}_{arch}"` as the value here. You could also use `"{os}_{arch}_freethreaded"` as well. Use `"*"` to evaluate the requirements for all of the platforms configured with -{obj}`pip.default`. +{obj}`pip.default`. When used together with {attr}`uv_lock`, the platform +independent (`none-any`) wheel of a package, if present, is also used for the +platforms that are not configured with {obj}`pip.default`. :::{include} /_includes/experimental_api.md :::
diff --git a/python/private/pypi/hub_builder.bzl b/python/private/pypi/hub_builder.bzl index df0c487..1f61a5e 100644 --- a/python/private/pypi/hub_builder.bzl +++ b/python/private/pypi/hub_builder.bzl
@@ -577,6 +577,7 @@ extra_pip_args = pip_attr.extra_pip_args, get_index_urls = self._get_index_urls.get(python_version), toml_decode = getattr(self._config, "toml_decode", None), + any_src = "*" in pip_attr.target_platforms, logger = logger, ) @@ -631,6 +632,29 @@ repo = repo, ) + if getattr(whl, "any_src", None): + # Use the platform independent wheel for the platforms that are + # not configured, so it has no target platforms and the more + # specific config settings take precedence. + _add_whl_library( + self, + python_version = python_version, + whl = whl, + repo = _whl_repo( + src = whl.any_src, + index_url = whl.index_url, + whl_library_args = whl_library_args, + download_only = pip_attr.download_only, + netrc = self._config.netrc or pip_attr.netrc, + use_downloader = True, + auth_patterns = self._config.auth_patterns or pip_attr.auth_patterns, + python_version = _major_minor_version(python_version), + is_multiple_versions = whl.is_multiple_versions, + interpreter = interpreter, + enable_pipstar_extract = enable_pipstar_extract, + ), + ) + def _common_args(self, module_ctx, *, pip_attr): # Construct args separately so that the lock file can be smaller and does not include unused # attrs.
diff --git a/python/private/pypi/parse_requirements.bzl b/python/private/pypi/parse_requirements.bzl index 2d8c4b4..7913602 100644 --- a/python/private/pypi/parse_requirements.bzl +++ b/python/private/pypi/parse_requirements.bzl
@@ -47,6 +47,7 @@ extract_url_srcs = True, uv_lock = None, toml_decode = None, + any_src = False, logger): """Get the requirements with platforms that the requirements apply to. @@ -71,6 +72,9 @@ requirements files if both are provided. toml_decode: {type}`callable | None` A function to decode TOML content (e.g. `toml.decode`). Required when `uv_lock` is provided. + any_src: {type}`bool` whether to add the `any_src` field to each + returned struct, which is the platform independent wheel of the + package or `None`. Only supported with `uv_lock`. logger: repo_utils.logger, a simple struct to log diagnostic messages. Returns: @@ -96,6 +100,7 @@ get_index_urls = get_index_urls, extract_url_srcs = extract_url_srcs, uv_lock = uv_lock, + any_src = any_src, logger = logger, ) @@ -126,11 +131,13 @@ get_index_urls, extract_url_srcs, uv_lock, + any_src, logger): """Parse requirements using uv.lock as the primary source.""" if uv_lock: return _parse_uv_lock_json( uv_lock = uv_lock, + any_src = any_src, all_platforms = _get_all_platforms(requirements_by_platform) if requirements_by_platform else sorted(platforms.keys()), platforms = platforms, extra_pip_args = extra_pip_args, @@ -220,7 +227,7 @@ queue.append(target) return result -def _parse_uv_lock_json(uv_lock, all_platforms, logger, extra_pip_args = None, platforms = {}): +def _parse_uv_lock_json(uv_lock, all_platforms, logger, extra_pip_args = None, platforms = {}, any_src = False): """Parse uv.lock JSON and build the same return structs as parse_requirements. Args: @@ -230,6 +237,8 @@ extra_pip_args: {type}`list[str] | None` Extra pip arguments to pass through. platforms: {type}`dict[str, struct]` A dict of platform name to platform info (containing `.env` with PEP 508 marker environment). + any_src: {type}`bool` whether to add the `any_src` field with the platform + independent wheel of each package. Returns: {type}`list[struct]` The same format as {func}`parse_requirements`. @@ -247,6 +256,7 @@ version = pkg["version"] norm_name = normalize_name(name) entry = uv_packages.setdefault(norm_name, { + "any_srcs": {}, "distribution": name, "resolved_srcs": [], "versions": {}, @@ -303,6 +313,35 @@ requirement = _uv_lock_git_requirement(pkg["source"]["git"]), ) + # The platform independent wheel, if there is one, which can be used + # as a fallback for the platforms that are not configured. + if any_src and candidates and pkg_platforms and platforms.get(pkg_platforms[0]): + platform = platforms[pkg_platforms[0]] + any_whl = select_whl( + whls = candidates, + python_version = platform.env.get("python_full_version", "3"), + whl_platform_tags = ["any"], + whl_abi_tags = ["none"], + implementation_name = platform.env.get("implementation_name", "cpython"), + limit = 1, + logger = logger, + ) + if any_whl: + entry["any_srcs"][any_whl.filename + any_whl.digest] = struct( + distribution = name, + extra_pip_args = extra_pip_args or [], + requirement_line = "{name}{extras}=={version}".format( + name = name, + extras = extra_str, + version = version, + ), + target_platforms = [], + filename = any_whl.filename, + digest = any_whl.digest, + url = any_whl.url, + yanked = None, + ) + plat_to_src = {} for p in pkg_platforms: platform = platforms.get(p) @@ -368,7 +407,7 @@ for src in info["resolved_srcs"]: for p in src.target_platforms: resolved_platforms[p] = None - item = struct( + item = dict( name = norm_name, # Only expose packages that resolved on all configured platforms. # Workspace/root members (e.g. `source = { virtual = "." }` or @@ -384,7 +423,11 @@ index_url = info["index_url"], srcs = info["resolved_srcs"], ) - ret.append(item) + if any_src: + # The platform independent wheel, if there is exactly one. + any_srcs = info["any_srcs"].values() + item["any_src"] = any_srcs[0] if len(any_srcs) == 1 else None + ret.append(struct(**item)) logger.debug(lambda: "Parsed {} packages from uv.lock".format(len(ret))) return ret
diff --git a/tests/pypi/hub_builder/hub_builder_tests.bzl b/tests/pypi/hub_builder/hub_builder_tests.bzl index 0a5c319..2470cf7 100644 --- a/tests/pypi/hub_builder/hub_builder_tests.bzl +++ b/tests/pypi/hub_builder/hub_builder_tests.bzl
@@ -183,7 +183,25 @@ os_name = "osx", arch_name = "aarch64", mock_files = { - "uv.lock": """{"package":[{"name":"simple","source":{"registry":"https://pypi.org/simple"},"version":"0.0.1","wheels":[{"hash":"sha256:deadbeef","url":"https://files.pythonhosted.org/packages/simple-0.0.1-py3-none-any.whl"}]}]}""", + "uv.lock": json.encode({"package": [ + { + "name": "plat", + "source": {"registry": "https://pypi.org/simple"}, + "version": "0.0.1", + "wheels": [ + {"hash": "sha256:deadb00f", "url": "https://files.pythonhosted.org/packages/plat-0.0.1-cp315-cp315-manylinux_2_17_x86_64.whl"}, + {"hash": "sha256:deadbeef", "url": "https://files.pythonhosted.org/packages/plat-0.0.1-py3-none-any.whl"}, + ], + }, + { + "name": "platonly", + "source": {"registry": "https://pypi.org/simple"}, + "version": "0.0.1", + "wheels": [ + {"hash": "sha256:deadbaaf", "url": "https://files.pythonhosted.org/packages/platonly-0.0.1-cp315-cp315-manylinux_2_17_x86_64.whl"}, + ], + }, + ]}), }, ), _parse( @@ -196,20 +214,39 @@ ) pypi = builder.build() - pypi.exposed_packages().contains_exactly(["simple"]) + # Only the packages that are available on all of the platforms are exposed. + pypi.exposed_packages().contains_exactly(["plat"]) pypi.whl_map().contains_exactly({ - "simple": { - "pypi_315_simple_py3_none_any_deadbeef": [ + "plat": { + "pypi_315_plat_cp315_cp315_manylinux_2_17_x86_64_deadb00f": [ + whl_config_setting( + target_platforms = ("cp315_linux_x86_64",), + version = "3.15", + ), + ], + "pypi_315_plat_py3_none_any_deadbeef": [ whl_config_setting( target_platforms = ( "cp315_linux_aarch64", - "cp315_linux_x86_64", "cp315_linux_x86_64_freethreaded", "cp315_osx_aarch64", "cp315_windows_aarch64", ), version = "3.15", ), + # The fallback for the platforms that are not configured. + whl_config_setting( + version = "3.15", + ), + ], + }, + # There is no platform independent wheel, so there is no fallback. + "platonly": { + "pypi_315_platonly_cp315_cp315_manylinux_2_17_x86_64_deadbaaf": [ + whl_config_setting( + target_platforms = ("cp315_linux_x86_64",), + version = "3.15", + ), ], }, })