Skip to content

Commit a5f45f4

Browse files
committed
move the implementation for the compatible version matcher
1 parent b84d109 commit a5f45f4

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

python/private/py_wheel_normalize_pep440.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,14 @@ def _version_le(left, right):
620620
_right = right.key(local = False)
621621
return _left < _right or _version_eq(left, right)
622622

623+
def _version_compatible(left, right):
624+
# https://peps.python.org/pep-0440/#compatible-release
625+
# Note, the ~= operator can be also expressed as:
626+
# >= V.N, == V.*
627+
head, _, _ = right.norm.partition(".")
628+
right_star = parse_version("{}.*".format(head))
629+
return left.ge(right) and left.eq(right_star)
630+
623631
def _first_non_none(*args):
624632
for arg in args:
625633
if arg != None:
@@ -714,6 +722,7 @@ def _new_version(*, epoch = 0, release, pre = "", post = "", dev = "", local = "
714722
gt = lambda x: _version_gt(self, x), # buildifier: disable=uninitialized
715723
le = lambda x: _version_le(self, x), # buildifier: disable=uninitialized
716724
ge = lambda x: _version_ge(self, x), # buildifier: disable=uninitialized
725+
compatible = lambda x: _version_compatible(self, x), # buildifier: disable=uninitialized
717726
str = lambda: norm,
718727
key = lambda *, local = True: _key(self, local = local), # buildifier: disable=uninitialized
719728
)

python/private/pypi/pep508_evaluate.bzl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,7 @@ def _version_expr(left, op, right):
388388
elif op == ">=":
389389
return _left.ge(_right)
390390
elif op == "~=":
391-
# https://peps.python.org/pep-0440/#compatible-release
392-
# Note, the ~= operator can be also expressed as:
393-
# >= V.N, == V.*
394-
head, _, _ = right.partition(".")
395-
_right_star = parse_version("{}.*".format(head))
396-
return _left.ge(_right) and _left.eq(_right_star)
391+
return _left.compatible(_right)
397392
else:
398393
return False # Let's just ignore the invalid ops
399394

tests/pypi/pep508/evaluate_tests.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@ _MISC_EXPRESSIONS = [
297297
_expr_case('python_version <= "1.7.2"', True, {"python_version": "1.7.1"}),
298298
_expr_case('python_version >= "1.7.1"', True, {"python_version": "1.7.1"}),
299299
_expr_case('python_version >= "1.7.0"', True, {"python_version": "1.7.1"}),
300+
# Compatible version tests:
301+
# https://packaging.python.org/en/latest/specifications/version-specifiers/#compatible-release
302+
_expr_case('python_version ~= "2.2"', True, {"python_version": "2.3"}),
303+
_expr_case('python_version ~= "2.2"', False, {"python_version": "2.1"}),
304+
_expr_case('python_version ~= "2.2.post3"', False, {"python_version": "2.2"}),
305+
_expr_case('python_version ~= "2.2.post3"', True, {"python_version": "2.3"}),
306+
_expr_case('python_version ~= "2.2.post3"', False, {"python_version": "3.0"}),
300307
]
301308

302309
def _misc_expressions(env):

0 commit comments

Comments
 (0)