Skip to content

Commit 3106315

Browse files
committed
create an eqq operator and simplify
1 parent a5f45f4 commit 3106315

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

python/private/py_wheel_normalize_pep440.bzl

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,14 @@ def _pad_zeros(release, n):
526526
release = list(release) + [0] * padding
527527
return tuple(release)
528528

529+
def _version_eqq(left, right):
530+
if left.is_prefix or right.is_prefix:
531+
fail(_prefix_err(left, "<", right))
532+
533+
# https://peps.python.org/pep-0440/#arbitrary-equality
534+
# > simple string equality operations
535+
return left.norm == right.norm
536+
529537
def _version_eq(left, right):
530538
if left.is_prefix and right.is_prefix:
531539
fail("Invalid comparison: both versions cannot be prefix matching")
@@ -552,7 +560,21 @@ def _version_eq(left, right):
552560
##and left.local == right.local
553561
)
554562

563+
def _version_ne(left, right):
564+
return not _version_eq(left, right)
565+
566+
def _prefix_err(left, op, right):
567+
if left.is_prefix or right.is_prefix:
568+
fail("PEP440: only '==' and '!=' operators can use prefix matching: {} {} {}".format(
569+
left,
570+
op,
571+
right,
572+
))
573+
555574
def _version_lt(left, right):
575+
if left.is_prefix or right.is_prefix:
576+
fail(_prefix_err(left, "<", right))
577+
556578
if left.epoch > right.epoch:
557579
return False
558580
elif left.epoch < right.epoch:
@@ -577,6 +599,9 @@ def _version_lt(left, right):
577599
return False
578600

579601
def _version_gt(left, right):
602+
if left.is_prefix or right.is_prefix:
603+
fail(_prefix_err(left, ">", right))
604+
580605
if left.epoch > right.epoch:
581606
return True
582607
elif left.epoch < right.epoch:
@@ -607,20 +632,29 @@ def _version_gt(left, right):
607632
return False
608633

609634
def _version_ge(left, right):
635+
if left.is_prefix or right.is_prefix:
636+
fail(_prefix_err(left, ">=", right))
637+
610638
# PEP440: simple order check
611639
# https://peps.python.org/pep-0440/#inclusive-ordered-comparison
612640
_left = left.key(local = False)
613641
_right = right.key(local = False)
614642
return _left > _right or _version_eq(left, right)
615643

616644
def _version_le(left, right):
645+
if left.is_prefix or right.is_prefix:
646+
fail(_prefix_err(left, "<=", right))
647+
617648
# PEP440: simple order check
618649
# https://peps.python.org/pep-0440/#inclusive-ordered-comparison
619650
_left = left.key(local = False)
620651
_right = right.key(local = False)
621652
return _left < _right or _version_eq(left, right)
622653

623654
def _version_compatible(left, right):
655+
if left.is_prefix or right.is_prefix:
656+
fail(_prefix_err(left, "~=", right))
657+
624658
# https://peps.python.org/pep-0440/#compatible-release
625659
# Note, the ~= operator can be also expressed as:
626660
# >= V.N, == V.*
@@ -717,11 +751,12 @@ def _new_version(*, epoch = 0, release, pre = "", post = "", dev = "", local = "
717751
is_prefix = is_prefix,
718752
norm = norm,
719753
eq = lambda x: _version_eq(self, x), # buildifier: disable=uninitialized
720-
ne = lambda x: not _version_eq(self, x), # buildifier: disable=uninitialized
721-
lt = lambda x: _version_lt(self, x), # buildifier: disable=uninitialized
754+
eqq = lambda x: _version_eqq(self, x), # buildifier: disable=uninitialized
755+
ge = lambda x: _version_ge(self, x), # buildifier: disable=uninitialized
722756
gt = lambda x: _version_gt(self, x), # buildifier: disable=uninitialized
723757
le = lambda x: _version_le(self, x), # buildifier: disable=uninitialized
724-
ge = lambda x: _version_ge(self, x), # buildifier: disable=uninitialized
758+
lt = lambda x: _version_lt(self, x), # buildifier: disable=uninitialized
759+
ne = lambda x: _version_ne(self, x), # buildifier: disable=uninitialized
725760
compatible = lambda x: _version_compatible(self, x), # buildifier: disable=uninitialized
726761
str = lambda: norm,
727762
key = lambda *, local = True: _key(self, local = local), # buildifier: disable=uninitialized

python/private/pypi/pep508_evaluate.bzl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,17 +368,13 @@ def _version_expr(left, op, right):
368368
# or `platform_release`, which vary depending on platform.
369369
return _env_expr(left, op, right)
370370

371-
if op == "!=":
371+
if op == "===":
372+
return _left.eqq(_right)
373+
elif op == "!=":
372374
return _left.ne(_right)
373375
elif op == "==":
374376
# Matching of major, minor, patch only
375377
return _left.eq(_right)
376-
elif left.endswith(".*") or right.endswith(".*"):
377-
fail("PEP440: only '==' and '!=' operators can use prefix matching: {} {} {}".format(
378-
left,
379-
op,
380-
right,
381-
))
382378
elif op == "<":
383379
return _left.lt(_right)
384380
elif op == ">":

0 commit comments

Comments
 (0)