refactor(pypi): do not parse the versions to select the wheels (#4557)

Only the major and the minor numbers of the Python version are needed to
match the Python tags of the wheels, so get them directly instead of
using a PEP 440 version parser.

This means that `select_whl`, which is used by `coverage_deps.bzl`
when `python/repositories.bzl` is loaded in a `WORKSPACE` file, will
not need `pypackaging.bzl` once `version.bzl` is replaced with it.
diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel
index 5d51f2a..1754c17 100644
--- a/python/private/pypi/BUILD.bazel
+++ b/python/private/pypi/BUILD.bazel
@@ -384,12 +384,6 @@
 )
 
 bzl_library(
-    name = "python_tag",
-    srcs = ["python_tag.bzl"],
-    deps = ["//python/private:version"],
-)
-
-bzl_library(
     name = "render_pkg_aliases",
     srcs = ["render_pkg_aliases.bzl"],
     deps = [
@@ -414,7 +408,6 @@
     deps = [
         ":parse_whl_name",
         ":python_tag",
-        "//python/private:version",
     ],
 )
 
@@ -694,6 +687,11 @@
 )
 
 bzl_library(
+    name = "python_tag",
+    srcs = ["python_tag.bzl"],
+)
+
+bzl_library(
     name = "version_from_filename",
     srcs = ["version_from_filename.bzl"],
 )
diff --git a/python/private/pypi/python_tag.bzl b/python/private/pypi/python_tag.bzl
index 224c5f9..076c95b 100644
--- a/python/private/pypi/python_tag.bzl
+++ b/python/private/pypi/python_tag.bzl
@@ -1,7 +1,5 @@
 "A simple utility function to get the python_tag from the implementation name"
 
-load("//python/private:version.bzl", "version")
-
 # Taken from
 # https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#python-tag
 _PY_TAGS = {
@@ -19,19 +17,16 @@
 
     Args:
         implementation_name: {type}`str` the implementation name, e.g. "cpython"
-        python_version: {type}`str` a version who can be parsed using PEP440 compliant
-            parser.
+        python_version: {type}`str` the Python version, e.g. `3.14` or
+            `3.14.1`.
 
     Returns:
         A {type}`str` that represents the python_tag with a version if the
             python_version is given.
     """
     if python_version:
-        v = version.parse(python_version, strict = True)
-        suffix = "{}{}".format(
-            v.release[0],
-            v.release[1] if len(v.release) > 1 else "",
-        )
+        major, minor = python_major_minor(python_version)
+        suffix = "{}{}".format(major, "" if minor == None else minor)
     else:
         suffix = ""
 
@@ -39,3 +34,32 @@
         _PY_TAGS.get(implementation_name, implementation_name),
         suffix,
     )
+
+def python_major_minor(python_version):
+    """Get the major and maybe minor numbers of a Python version.
+
+    Only the first two numbers of the version are needed to match the
+    wheel tags, so the version is not parsed as per PEP 440.
+
+    Args:
+        python_version: {type}`str` the Python version, e.g. `3.14`,
+            `3.14.1` or `3.15.0rc1`.
+
+    Returns:
+        A {type}`tuple[int, int | None]` of the major and minor numbers, the
+        minor is `None` if the version does not have it.
+    """
+    major, _, tail = python_version.partition(".")
+    tail, _, _ = tail.partition(".")
+
+    # Drop what may follow the minor number, e.g. `rc1` in `3.15rc1`
+    minor = ""
+    for c in tail.elems():
+        if not c.isdigit():
+            break
+        minor += c
+
+    if not major.isdigit() or (tail and not minor):
+        fail("Expected the Python version to start with '<major>.<minor>', got: '{}'".format(python_version))
+
+    return (int(major), int(minor) if minor else None)
diff --git a/python/private/pypi/select_whl.bzl b/python/private/pypi/select_whl.bzl
index b32fc68..f8215e5 100644
--- a/python/private/pypi/select_whl.bzl
+++ b/python/private/pypi/select_whl.bzl
@@ -1,8 +1,7 @@
 "Select a single wheel that fits the parameters of a target platform."
 
-load("//python/private:version.bzl", "version")
 load(":parse_whl_name.bzl", "parse_whl_name")
-load(":python_tag.bzl", "PY_TAG_GENERIC", "python_tag")
+load(":python_tag.bzl", "PY_TAG_GENERIC", "python_major_minor", "python_tag")
 
 _ANDROID = "android"
 _IOS = "ios"
@@ -152,16 +151,17 @@
     else:
         return None
 
-    # Add a 0 at the end in case it is a single digit
-    ver_str = "{}.{}".format(ver_str[0], ver_str[1:] or "0")
+    if not ver_str.isdigit():
+        return None
 
-    ver = version.parse(ver_str)
-    if not version.is_compatible(py_version, ver):
+    # The minor is 0 if the tag only has the major version, e.g. `py3`
+    ver = (int(ver_str[0]), int(ver_str[1:] or "0"))
+    if ver[0] != py_version[0] or ver > py_version:
         return None
 
     return (
         tag.startswith(implementation),
-        version.key(ver),
+        ver,
     )
 
 def _candidates_by_priority(
@@ -178,7 +178,8 @@
         A dictionary where keys are priority tuples which allows us to sort and pick the
         last item.
     """
-    py_version = version.parse(python_version, strict = True)
+    major, minor = python_major_minor(python_version)
+    py_version = (major, minor or 0)
     implementation = python_tag(implementation_name)
 
     ret = {}
@@ -206,7 +207,7 @@
                     logger.debug(lambda: "The python_tag in '{}' does not match implementation or version: {} {}".format(
                         whl.filename,
                         implementation,
-                        py_version.string,
+                        python_version,
                     ))
                     continue
 
diff --git a/tests/pypi/python_tag/python_tag_tests.bzl b/tests/pypi/python_tag/python_tag_tests.bzl
index ca86575..2eaa0c2 100644
--- a/tests/pypi/python_tag/python_tag_tests.bzl
+++ b/tests/pypi/python_tag/python_tag_tests.bzl
@@ -20,8 +20,15 @@
 _tests.append(_test_without_version)
 
 def _test_with_version(env):
-    got = python_tag("cpython", "3.1.15")
-    env.expect.that_str(got).equals("cp31")
+    for give, expect in {
+        "3": "cp3",
+        "3.1.15": "cp31",
+        "3.15": "cp315",
+        "3.15.0rc1": "cp315",
+        "3.15rc1": "cp315",
+    }.items():
+        got = python_tag("cpython", give)
+        env.expect.that_str(got).equals(expect)
 
 _tests.append(_test_with_version)