feat(pypi): select abi3t wheels on free-threaded platforms (#4496)

The free-threaded builds could not select the wheels that use the stable
ABI
for the free-threaded builds (`abi3t`) as specified in
[PEP 803](https://peps.python.org/pep-0803/).

With this change the default `_freethreaded` platforms use the
`["abi3t", "cp{major}{minor}t"]` ABI tags, so `cp3xt` wheels are still
preferred over `abi3t` wheels, in the same way as `cp3x` wheels are
preferred
over `abi3` wheels. The python tag of an `abi3t` wheel is the minimum
supported version, e.g. `cp315-abi3t` is selected for Python 3.15 and
above,
and the GIL-enabled builds only select such wheels if they are also
tagged as
`abi3` (e.g. `cp315-abi3.abi3t`). `abi3t` is also treated as a stable
ABI tag
when deriving the target platforms from the wheel tags.

---------

Co-authored-by: Richard Levasseur <richardlev@gmail.com>
diff --git a/news/abi3t-whl-selection.added.md b/news/abi3t-whl-selection.added.md
new file mode 100644
index 0000000..2f76e1c
--- /dev/null
+++ b/news/abi3t-whl-selection.added.md
@@ -0,0 +1,4 @@
+(pypi) The default `_freethreaded` {obj}`pip.default` platforms now select the
+`abi3t` wheels that use the stable ABI for the free-threaded builds as specified
+in [PEP 803](https://peps.python.org/pep-0803/). The version-specific `cp3xt`
+wheels are still preferred over `abi3t` wheels.
diff --git a/python/private/pypi/default_platforms.bzl b/python/private/pypi/default_platforms.bzl
index d303272..8d3b119 100644
--- a/python/private/pypi/default_platforms.bzl
+++ b/python/private/pypi/default_platforms.bzl
@@ -12,6 +12,19 @@
         os_constraint = None,
         config_settings = [],
         marker = ""):
+    # The free-threaded builds can use the `abi3t` stable ABI wheels, see
+    # https://peps.python.org/pep-0803/. The python tag of the `abi3t`
+    # wheels is the minimum supported version, which is handled in the same
+    # way as for `abi3` when selecting the wheels. The GIL-enabled builds
+    # cannot use `abi3t` wheels unless they are also tagged with `abi3`,
+    # e.g. `cp315-abi3.abi3t`.
+    #
+    # The last item has the highest priority.
+    if freethreaded:
+        whl_abi_tags = ["abi3t", "cp{major}{minor}t"]
+    else:
+        whl_abi_tags = ["abi3", "cp{major}{minor}"]
+
     return {
         "arch_name": arch_name,
         "config_settings": [
@@ -26,10 +39,7 @@
         "marker": "python_version >= '3.13'" if freethreaded else marker,
         "name": name,
         "os_name": os_name,
-        "whl_abi_tags": ["cp{major}{minor}t"] if freethreaded else [
-            "abi3",
-            "cp{major}{minor}",
-        ],
+        "whl_abi_tags": whl_abi_tags,
         "whl_platform_tags": whl_platform_tags,
     }
 
diff --git a/python/private/pypi/extension.bzl b/python/private/pypi/extension.bzl
index bb63503..21d5ba5 100644
--- a/python/private/pypi/extension.bzl
+++ b/python/private/pypi/extension.bzl
@@ -685,6 +685,18 @@
 `cp{major}{minor}` will result in `cp313` given the full python version is `3.13.5`.
 Will always  include `"none"` even if it is not specified.
 
+The python tag of the wheels using the stable ABI (`abi3` and `abi3t`) is
+treated as the minimum supported Python version, e.g. a `cp315-abi3t` wheel is
+selected for Python `3.15` and above.
+
+The default platforms use the following values:
+* `["abi3", "cp{major}{minor}"]` for the GIL-enabled builds.
+* `["abi3t", "cp{major}{minor}t"]` for the free-threaded builds, where `abi3t`
+  is the stable ABI for the free-threaded builds as specified in
+  [PEP 803](https://peps.python.org/pep-0803/). The GIL-enabled builds only
+  select `abi3t` wheels if they are also tagged as `abi3` (e.g.
+  `cp315-abi3.abi3t`).
+
 :::{note}
 We select a single wheel and the last match will take precedence.
 :::
@@ -692,6 +704,10 @@
 :::{seealso}
 See official [docs](https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#abi-tag) for more information.
 :::
+
+:::{versionchanged} VERSION_NEXT_FEATURE
+The default free-threaded platforms select the `abi3t` wheels.
+:::
 """,
     ),
     "whl_platform_tags": attr.string_list(
diff --git a/python/private/pypi/whl_target_platforms.bzl b/python/private/pypi/whl_target_platforms.bzl
index 28547c6..b48b3bb 100644
--- a/python/private/pypi/whl_target_platforms.bzl
+++ b/python/private/pypi/whl_target_platforms.bzl
@@ -64,8 +64,14 @@
     cpus = _cpu_from_tag(platform_tag)
 
     abi = None
-    if abi_tag not in ["", "none", "abi3"]:
-        abi = abi_tag
+
+    # Stable ABI tags (`abi3`, `abi3t`, and `abi3.abi3t`; see
+    # https://peps.python.org/pep-0803/) do not identify a single
+    # interpreter ABI.
+    for tag in abi_tag.split("."):
+        if tag not in ["", "none", "abi3", "abi3t"]:
+            abi = abi_tag
+            break
 
     # TODO @aignas 2024-05-29: this code is present in many places, I think
     _, _, tail = platform_tag.partition("_")
diff --git a/tests/pypi/extension/extension_tests.bzl b/tests/pypi/extension/extension_tests.bzl
index b14e6fe..91bf3c7 100644
--- a/tests/pypi/extension/extension_tests.bzl
+++ b/tests/pypi/extension/extension_tests.bzl
@@ -75,6 +75,10 @@
         whl_platform_tags = whl_platform_tags or [],
     )
 
+def _default_whl_abi_tags(os, cpu, freethreaded):
+    platform = "{}_{}{}".format(os, cpu, freethreaded)
+    return default_platforms()[platform]["whl_abi_tags"]
+
 # The default value for the default platforms tags use in `_mod`.
 _default_tags_default = [
     _default(
@@ -85,7 +89,7 @@
             "@platforms//os:{}".format(os),
             "@platforms//cpu:{}".format(cpu),
         ],
-        whl_abi_tags = ["cp{major}{minor}t"] if freethreaded else ["abi3", "cp{major}{minor}"],
+        whl_abi_tags = _default_whl_abi_tags(os, cpu, freethreaded),
         whl_platform_tags = whl_platform_tags,
     )
     for (os, cpu, freethreaded), whl_platform_tags in {
diff --git a/tests/pypi/hub_builder/hub_builder_tests.bzl b/tests/pypi/hub_builder/hub_builder_tests.bzl
index fca32c5..dd7cd65 100644
--- a/tests/pypi/hub_builder/hub_builder_tests.bzl
+++ b/tests/pypi/hub_builder/hub_builder_tests.bzl
@@ -17,6 +17,7 @@
 load("@rules_testing//lib:test_suite.bzl", "test_suite")
 load("@rules_testing//lib:truth.bzl", "subjects")
 load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "REPO_VERBOSITY_ENV_VAR", "repo_utils")  # buildifier: disable=bzl-visibility
+load("//python/private/pypi:default_platforms.bzl", "default_platforms")  # buildifier: disable=bzl-visibility
 load("//python/private/pypi:hub_builder.bzl", _hub_builder = "hub_builder")  # buildifier: disable=bzl-visibility
 load("//python/private/pypi:parse_simpleapi_html.bzl", "parse_simpleapi_html")  # buildifier: disable=bzl-visibility
 load("//python/private/pypi:platform.bzl", _plat = "platform")  # buildifier: disable=bzl-visibility
@@ -40,6 +41,10 @@
         },
     )
 
+def _default_whl_abi_tags(os, cpu, freethreaded):
+    platform = "{}_{}{}".format(os, cpu, freethreaded)
+    return default_platforms()[platform]["whl_abi_tags"]
+
 def hub_builder(
         env,
         enable_pipstar_extract = True,
@@ -66,7 +71,7 @@
                         "@platforms//os:{}".format(os),
                         "@platforms//cpu:{}".format(cpu),
                     ],
-                    whl_abi_tags = ["cp{major}{minor}t"] if freethreaded else ["abi3", "cp{major}{minor}"],
+                    whl_abi_tags = _default_whl_abi_tags(os, cpu, freethreaded),
                     whl_platform_tags = whl_platform_tags,
                 )
                 for (os, cpu, freethreaded), whl_platform_tags in {
diff --git a/tests/pypi/select_whl/select_whl_tests.bzl b/tests/pypi/select_whl/select_whl_tests.bzl
index 8d2170e..422c93c 100644
--- a/tests/pypi/select_whl/select_whl_tests.bzl
+++ b/tests/pypi/select_whl/select_whl_tests.bzl
@@ -2,6 +2,7 @@
 
 load("@rules_testing//lib:test_suite.bzl", "test_suite")
 load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "REPO_VERBOSITY_ENV_VAR", "repo_utils")  # buildifier: disable=bzl-visibility
+load("//python/private/pypi:default_platforms.bzl", "default_platforms")  # buildifier: disable=bzl-visibility
 load("//python/private/pypi:select_whl.bzl", "select_whl")  # buildifier: disable=bzl-visibility
 
 WHL_LIST = [
@@ -61,6 +62,17 @@
     "pkg-0.0.1-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
 ]
 
+# See https://peps.python.org/pep-0803/
+ABI3T_WHL_LIST = [
+    "pkg-0.0.1-py3-none-any.whl",
+    "pkg-0.0.1-cp315-abi3-manylinux_2_17_x86_64.whl",
+    "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+    "pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
+    "pkg-0.0.1-cp315-cp315-manylinux_2_17_x86_64.whl",
+    "pkg-0.0.1-cp315-cp315t-manylinux_2_17_x86_64.whl",
+    "pkg-0.0.1-cp316-cp316t-manylinux_2_17_x86_64.whl",
+]
+
 def _match(env, got, *want_filenames):
     if not want_filenames:
         env.expect.that_collection(got).has_size(len(want_filenames))
@@ -92,6 +104,13 @@
         **kwargs
     )
 
+def _default_abi_tags(platform, python_version):
+    major, _, minor = python_version.partition(".")
+    return ["none"] + [
+        tag.format(major = major, minor = minor)
+        for tag in default_platforms()[platform]["whl_abi_tags"]
+    ]
+
 _tests = []
 
 def _test_not_select_py2(env):
@@ -547,6 +566,151 @@
 
 _tests.append(_test_android)
 
+def _test_abi3t_freethreaded(env):
+    got = _select_whl(
+        whls = [
+            "pkg-0.0.1-py3-none-any.whl",
+            "pkg-0.0.1-cp315-abi3-manylinux_2_17_x86_64.whl",
+            "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+            "pkg-0.0.1-cp315-cp315-manylinux_2_17_x86_64.whl",
+        ],
+        whl_platform_tags = ["any", "manylinux_*_x86_64"],
+        whl_abi_tags = _default_abi_tags("linux_x86_64_freethreaded", "3.15"),
+        python_version = "3.15",
+        limit = 3,
+    )
+    _match(
+        env,
+        got,
+        # The free-threaded builds cannot use `abi3` or `cp315` wheels.
+        "pkg-0.0.1-py3-none-any.whl",
+        "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+    )
+
+_tests.append(_test_abi3t_freethreaded)
+
+def _test_abi3t_prefer_version_specific(env):
+    got = _select_whl(
+        whls = ABI3T_WHL_LIST,
+        whl_platform_tags = ["any", "manylinux_*_x86_64"],
+        whl_abi_tags = _default_abi_tags("linux_x86_64_freethreaded", "3.15"),
+        python_version = "3.15",
+        limit = 3,
+    )
+    _match(
+        env,
+        got,
+        # The last item has the most priority
+        "pkg-0.0.1-py3-none-any.whl",
+        "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+        "pkg-0.0.1-cp315-cp315t-manylinux_2_17_x86_64.whl",
+    )
+
+_tests.append(_test_abi3t_prefer_version_specific)
+
+def _test_abi3t_python_tag_is_minimum_version(env):
+    # The `cp315-abi3t` wheel can be used on 3.15 and above, whilst the
+    # `cp316-abi3t` wheel cannot be used on 3.15.
+    for python_version, want in {
+        "3.14": [
+            "pkg-0.0.1-py3-none-any.whl",
+        ],
+        "3.15": [
+            "pkg-0.0.1-py3-none-any.whl",
+            "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+        ],
+        "3.16": [
+            "pkg-0.0.1-py3-none-any.whl",
+            "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+            "pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
+        ],
+        "3.17": [
+            "pkg-0.0.1-py3-none-any.whl",
+            "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+            "pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
+        ],
+    }.items():
+        got = _select_whl(
+            whls = [
+                "pkg-0.0.1-py3-none-any.whl",
+                "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+                "pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
+            ],
+            whl_platform_tags = ["any", "manylinux_*_x86_64"],
+            whl_abi_tags = _default_abi_tags(
+                "linux_x86_64_freethreaded",
+                python_version,
+            ),
+            python_version = python_version,
+            limit = 3,
+        )
+        _match(env, got, *want)
+
+_tests.append(_test_abi3t_python_tag_is_minimum_version)
+
+def _test_abi3t_newer_cpxt_preferred(env):
+    got = _select_whl(
+        whls = ABI3T_WHL_LIST,
+        whl_platform_tags = ["any", "manylinux_*_x86_64"],
+        whl_abi_tags = _default_abi_tags("linux_x86_64_freethreaded", "3.16"),
+        python_version = "3.16",
+        limit = 4,
+    )
+    _match(
+        env,
+        got,
+        # The last item has the most priority
+        "pkg-0.0.1-py3-none-any.whl",
+        "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+        "pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
+        "pkg-0.0.1-cp316-cp316t-manylinux_2_17_x86_64.whl",
+    )
+
+_tests.append(_test_abi3t_newer_cpxt_preferred)
+
+def _test_abi3t_not_selected_on_gil_builds(env):
+    got = _select_whl(
+        whls = [
+            "pkg-0.0.1-py3-none-any.whl",
+            "pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
+            "pkg-0.0.1-cp315-cp315t-manylinux_2_17_x86_64.whl",
+        ],
+        whl_platform_tags = ["any", "manylinux_*_x86_64"],
+        whl_abi_tags = _default_abi_tags("linux_x86_64", "3.15"),
+        python_version = "3.15",
+        limit = 3,
+    )
+    _match(
+        env,
+        got,
+        # The GIL-enabled builds cannot use `abi3t` only wheels.
+        "pkg-0.0.1-py3-none-any.whl",
+    )
+
+_tests.append(_test_abi3t_not_selected_on_gil_builds)
+
+def _test_abi3_abi3t_compressed_tag(env):
+    whls = [
+        "pkg-0.0.1-py3-none-any.whl",
+        "pkg-0.0.1-cp315-abi3.abi3t-manylinux_2_17_x86_64.whl",
+    ]
+    for platform in ["linux_x86_64", "linux_x86_64_freethreaded"]:
+        got = _select_whl(
+            whls = whls,
+            whl_platform_tags = ["any", "manylinux_*_x86_64"],
+            whl_abi_tags = _default_abi_tags(platform, "3.15"),
+            python_version = "3.15",
+        )
+
+        # The wheels compatible with both ABIs can be used on both builds.
+        _match(
+            env,
+            [got],
+            "pkg-0.0.1-cp315-abi3.abi3t-manylinux_2_17_x86_64.whl",
+        )
+
+_tests.append(_test_abi3_abi3t_compressed_tag)
+
 def select_whl_test_suite(name):
     """Create the test suite.
 
diff --git a/tests/pypi/whl_target_platforms/whl_target_platforms_tests.bzl b/tests/pypi/whl_target_platforms/whl_target_platforms_tests.bzl
index 6bec26c..7c0d0ad 100644
--- a/tests/pypi/whl_target_platforms/whl_target_platforms_tests.bzl
+++ b/tests/pypi/whl_target_platforms/whl_target_platforms_tests.bzl
@@ -43,7 +43,7 @@
     }
 
     for give, want in tests.items():
-        for abi in ["", "abi3", "none"]:
+        for abi in ["", "abi3", "abi3t", "abi3.abi3t", "none"]:
             got = whl_target_platforms(give, abi)
             env.expect.that_collection(got).contains_exactly(want)