Skip to content

Commit 22da480

Browse files
committed
Add a new file for parsing versions
1 parent 3106315 commit 22da480

8 files changed

Lines changed: 20 additions & 36 deletions

File tree

python/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ bzl_library(
9393
"//python/private:bzlmod_enabled_bzl",
9494
"//python/private:py_package.bzl",
9595
"//python/private:py_wheel_bzl",
96-
"//python/private:py_wheel_normalize_pep440.bzl",
9796
"//python/private:stamp_bzl",
9897
"//python/private:util_bzl",
98+
"//python/private:version.bzl",
9999
"@bazel_skylib//rules:native_binary",
100100
],
101101
)

python/private/BUILD.bazel

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,6 @@ bzl_library(
578578
],
579579
)
580580

581-
bzl_library(
582-
name = "py_wheel_normalize_pep440_bzl",
583-
srcs = ["py_wheel_normalize_pep440.bzl"],
584-
)
585-
586581
bzl_library(
587582
name = "reexports_bzl",
588583
srcs = ["reexports.bzl"],
@@ -663,6 +658,11 @@ bzl_library(
663658
],
664659
)
665660

661+
bzl_library(
662+
name = "version_bzl",
663+
srcs = ["version.bzl"],
664+
)
665+
666666
bzl_library(
667667
name = "version_label_bzl",
668668
srcs = ["version_label.bzl"],
@@ -706,7 +706,7 @@ exports_files(
706706
"repack_whl.py",
707707
"py_package.bzl",
708708
"py_wheel.bzl",
709-
"py_wheel_normalize_pep440.bzl",
709+
"version.bzl",
710710
"reexports.bzl",
711711
"stamp.bzl",
712712
"util.bzl",

python/private/py_wheel.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
load(":py_info.bzl", "PyInfo")
1818
load(":py_package.bzl", "py_package_lib")
19-
load(":py_wheel_normalize_pep440.bzl", "normalize_pep440")
2019
load(":stamp.bzl", "is_stamping_enabled")
20+
load(":version.bzl", "normalize_pep440")
2121

2222
PyWheelInfo = provider(
2323
doc = "Information about a wheel produced by `py_wheel`",

python/private/pypi/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ bzl_library(
232232
srcs = ["pep508_env.bzl"],
233233
deps = [
234234
":pep508_platform_bzl",
235-
"//python/private:py_wheel_normalize_pep440_bzl",
235+
"//python/private:version_bzl",
236236
],
237237
)
238238

python/private/pypi/pep508_evaluate.bzl

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
"""
1717

1818
load("//python/private:enum.bzl", "enum")
19-
load("//python/private:py_wheel_normalize_pep440.bzl", "parse_version")
20-
21-
# TODO @aignas 2025-05-06: this is exposed for tests only
22-
version = parse_version
19+
load("//python/private:version.bzl", "version")
2320

2421
# The expression parsing and resolution for the PEP508 is below
2522
#
@@ -348,20 +345,8 @@ def _env_expr(left, op, right):
348345

349346
def _version_expr(left, op, right):
350347
"""Evaluate a version comparison expression"""
351-
if op == "===":
352-
# https://peps.python.org/pep-0440/#arbitrary-equality
353-
# > simple string equality operations
354-
return _env_expr(left, "==", right)
355-
356-
if left.endswith(".*") and right.endswith(".*"):
357-
fail("PEP440: only one of the sides can have '.*' suffix: {} {} {}".format(
358-
left,
359-
op,
360-
right,
361-
))
362-
363-
_left = parse_version(left)
364-
_right = parse_version(right)
348+
_left = version(left)
349+
_right = version(right)
365350
if _left == None or _right == None:
366351
# Per spec, if either can't be normalized to a version, then
367352
# fallback to simple string comparison. Usually this is `platform_version`

python/private/py_wheel_normalize_pep440.bzl renamed to python/private/version.bzl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ def _version_compatible(left, right):
659659
# Note, the ~= operator can be also expressed as:
660660
# >= V.N, == V.*
661661
head, _, _ = right.norm.partition(".")
662-
right_star = parse_version("{}.*".format(head))
662+
right_star = version("{}.*".format(head))
663663
return left.ge(right) and left.eq(right_star)
664664

665665
def _first_non_none(*args):
@@ -764,23 +764,21 @@ def _new_version(*, epoch = 0, release, pre = "", post = "", dev = "", local = "
764764

765765
return self
766766

767-
def parse_version(version, strict = False):
767+
def version(version_str, strict = False):
768768
"""Parse a PEP4408 compliant version
769769
770-
TODO @aignas 2025-05-06: where should this go?
771-
772770
See https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode
773771
and https://peps.python.org/pep-0440/
774772
775773
Args:
776-
version: version string to be normalized according to PEP 440.
774+
version_str: version string to be normalized according to PEP 440.
777775
strict: fail if the version is invalid.
778776
779777
Returns:
780778
string containing the normalized version.
781779
"""
782780

783-
parser = _new(version.strip(" " if strict else " .*")) # PEP 440: Leading and Trailing Whitespace and .*
781+
parser = _new(version_str.strip(" " if strict else " .*")) # PEP 440: Leading and Trailing Whitespace and .*
784782

785783
accept(parser, _is("v"), "") # PEP 440: Preceding v character
786784

@@ -799,7 +797,7 @@ def parse_version(version, strict = False):
799797
parts[p] = parser.context()["norm"]
800798
parser.context()["norm"] = "" # Clear out the buffer so that it is easy to separate the fields
801799

802-
is_prefix = version.endswith(".*")
800+
is_prefix = version_str.endswith(".*")
803801
parts["is_prefix"] = is_prefix
804802
if is_prefix and (parts["local"] or parts["post"] or parts["dev"] or parts["pre"]):
805803
if strict:

tests/py_wheel/py_wheel_tests.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
1717
load("@rules_testing//lib:truth.bzl", "matching")
1818
load("@rules_testing//lib:util.bzl", rt_util = "util")
1919
load("//python:packaging.bzl", "py_wheel")
20-
load("//python/private:py_wheel_normalize_pep440.bzl", "normalize_pep440") # buildifier: disable=bzl-visibility
20+
load("//python/private:version.bzl", "normalize_pep440") # buildifier: disable=bzl-visibility
2121

2222
_basic_tests = []
2323
_tests = []

tests/pypi/pep508/evaluate_tests.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
"""Tests for construction of Python version matching config settings."""
1515

1616
load("@rules_testing//lib:test_suite.bzl", "test_suite")
17+
load("//python/private:version.bzl", "version") # buildifier: disable=bzl-visibility
1718
load("//python/private/pypi:pep508_env.bzl", pep508_env = "env") # buildifier: disable=bzl-visibility
18-
load("//python/private/pypi:pep508_evaluate.bzl", "evaluate", "tokenize", "version") # buildifier: disable=bzl-visibility
19+
load("//python/private/pypi:pep508_evaluate.bzl", "evaluate", "tokenize") # buildifier: disable=bzl-visibility
1920

2021
_tests = []
2122

0 commit comments

Comments
 (0)