Skip to content

Commit 14cd842

Browse files
committed
Fix #236: add missing deprecated functions
Deprecate: * `semver.compare` -> `semver.VersionInfo.compare` * `semver.match` -> `semver.VersionInfo.match` Change: * Implementation of `semver.max_ver` and `semver.min_ver`
1 parent 9691b41 commit 14cd842

File tree

2 files changed

+109
-38
lines changed

2 files changed

+109
-38
lines changed

semver.py

Lines changed: 107 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,50 @@ def bump_build(self, token="build"):
371371
build = cls._increment_string(self._build or (token or "build") + ".0")
372372
return cls(self._major, self._minor, self._patch, self._prerelease, build)
373373

374+
def compare(self, other):
375+
"""
376+
Compare self with other.
377+
378+
:param other: second version (can be string or a VersionInfo instance)
379+
:return: The return value is negative if ver1 < ver2,
380+
zero if ver1 == ver2 and strictly positive if ver1 > ver2
381+
:rtype: int
382+
383+
>>> semver.VersionInfo.parse("1.0.0").compare("2.0.0")
384+
-1
385+
>>> semver.VersionInfo.parse("2.0.0").compare("1.0.0")
386+
1
387+
>>> semver.VersionInfo.parse("2.0.0").compare("2.0.0")
388+
0
389+
"""
390+
cls = type(self)
391+
if isinstance(other, str):
392+
other = cls.parse(other)
393+
elif not isinstance(other, cls):
394+
raise TypeError(
395+
"Expected str or {} instance, but got {}".format(
396+
cls.__name__, type(other)
397+
)
398+
)
399+
400+
v1 = self.to_tuple()[:3]
401+
v2 = other.to_tuple()[:3]
402+
x = cmp(v1, v2)
403+
if x:
404+
return x
405+
406+
rc1, rc2 = self.prerelease, other.prerelease
407+
rccmp = _nat_cmp(rc1, rc2)
408+
409+
if not rccmp:
410+
return 0
411+
if not rc1:
412+
return 1
413+
elif not rc2:
414+
return -1
415+
416+
return rccmp
417+
374418
@comparator
375419
def __eq__(self, other):
376420
return _compare_by_keys(self.to_dict(), _to_dict(other)) == 0
@@ -424,6 +468,53 @@ def finalize_version(self):
424468
cls = type(self)
425469
return cls(self.major, self.minor, self.patch)
426470

471+
def match(self, match_expr):
472+
"""
473+
Compare self to match a match expression.
474+
475+
:param str match_expr: operator and version; valid operators are
476+
< smaller than
477+
> greater than
478+
>= greator or equal than
479+
<= smaller or equal than
480+
== equal
481+
!= not equal
482+
:return: True if the expression matches the version, otherwise False
483+
:rtype: bool
484+
485+
>>> semver.VersionInfo.parse("2.0.0").match(">=1.0.0")
486+
True
487+
>>> semver.VersionInfo.parse("1.0.0").match(">1.0.0")
488+
False
489+
"""
490+
prefix = match_expr[:2]
491+
if prefix in (">=", "<=", "==", "!="):
492+
match_version = match_expr[2:]
493+
elif prefix and prefix[0] in (">", "<"):
494+
prefix = prefix[0]
495+
match_version = match_expr[1:]
496+
else:
497+
raise ValueError(
498+
"match_expr parameter should be in format <op><ver>, "
499+
"where <op> is one of "
500+
"['<', '>', '==', '<=', '>=', '!=']. "
501+
"You provided: %r" % match_expr
502+
)
503+
504+
possibilities_dict = {
505+
">": (1,),
506+
"<": (-1,),
507+
"==": (0,),
508+
"!=": (-1, 1),
509+
">=": (0, 1),
510+
"<=": (-1, 0),
511+
}
512+
513+
possibilities = possibilities_dict[prefix]
514+
cmp_res = self.compare(match_version)
515+
516+
return cmp_res in possibilities
517+
427518
@staticmethod
428519
def parse(version):
429520
"""
@@ -579,6 +670,7 @@ def _compare_by_keys(d1, d2):
579670
return rccmp
580671

581672

673+
@deprecated(version="2.10.0")
582674
def compare(ver1, ver2):
583675
"""
584676
Compare two versions strings.
@@ -596,13 +688,13 @@ def compare(ver1, ver2):
596688
>>> semver.compare("2.0.0", "2.0.0")
597689
0
598690
"""
599-
600-
v1 = VersionInfo.parse(ver1).to_dict()
601-
v2 = VersionInfo.parse(ver2).to_dict()
602-
603-
return _compare_by_keys(v1, v2)
691+
v1 = VersionInfo.parse(ver1)
692+
# v2 = VersionInfo.parse(ver2)
693+
return v1.compare(ver2)
694+
# return _compare_by_keys(v1, v2)
604695

605696

697+
@deprecated(version="2.10.0")
606698
def match(version, match_expr):
607699
"""
608700
Compare two versions strings through a comparison.
@@ -623,33 +715,8 @@ def match(version, match_expr):
623715
>>> semver.match("1.0.0", ">1.0.0")
624716
False
625717
"""
626-
prefix = match_expr[:2]
627-
if prefix in (">=", "<=", "==", "!="):
628-
match_version = match_expr[2:]
629-
elif prefix and prefix[0] in (">", "<"):
630-
prefix = prefix[0]
631-
match_version = match_expr[1:]
632-
else:
633-
raise ValueError(
634-
"match_expr parameter should be in format <op><ver>, "
635-
"where <op> is one of "
636-
"['<', '>', '==', '<=', '>=', '!=']. "
637-
"You provided: %r" % match_expr
638-
)
639-
640-
possibilities_dict = {
641-
">": (1,),
642-
"<": (-1,),
643-
"==": (0,),
644-
"!=": (-1, 1),
645-
">=": (0, 1),
646-
"<=": (-1, 0),
647-
}
648-
649-
possibilities = possibilities_dict[prefix]
650-
cmp_res = compare(version, match_version)
651-
652-
return cmp_res in possibilities
718+
ver = VersionInfo.parse(version)
719+
return ver.match(match_expr)
653720

654721

655722
def max_ver(ver1, ver2):
@@ -664,9 +731,10 @@ def max_ver(ver1, ver2):
664731
>>> semver.max_ver("1.0.0", "2.0.0")
665732
'2.0.0'
666733
"""
667-
cmp_res = compare(ver1, ver2)
668-
if cmp_res == 0 or cmp_res == 1:
669-
return ver1
734+
ver1 = VersionInfo.parse(ver1)
735+
cmp_res = ver1.compare(ver2)
736+
if cmp_res >= 0:
737+
return str(ver1)
670738
else:
671739
return ver2
672740

@@ -683,9 +751,10 @@ def min_ver(ver1, ver2):
683751
>>> semver.min_ver("1.0.0", "2.0.0")
684752
'1.0.0'
685753
"""
686-
cmp_res = compare(ver1, ver2)
687-
if cmp_res == 0 or cmp_res == -1:
688-
return ver1
754+
ver1 = VersionInfo.parse(ver1)
755+
cmp_res = ver1.compare(ver2)
756+
if cmp_res <= 0:
757+
return str(ver1)
689758
else:
690759
return ver2
691760

test_semver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,10 @@ def test_should_versioninfo_isvalid():
855855
(bump_minor, ("1.2.3",), {}),
856856
(bump_patch, ("1.2.3",), {}),
857857
(bump_prerelease, ("1.2.3",), {}),
858+
(compare, ("1.2.1", "1.2.2"), {}),
858859
(format_version, (3, 4, 5), {}),
859860
(finalize_version, ("1.2.3-rc.5",), {}),
861+
(match, ("1.0.0", ">=1.0.0"), {}),
860862
(parse, ("1.2.3",), {}),
861863
(parse_version_info, ("1.2.3",), {}),
862864
(replace, ("1.2.3",), dict(major=2, patch=10)),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy