Skip to content

Commit 4482f8c

Browse files
committed
Get rid of _compare_by_keys, adapt comparison operators
* Call self.compare(other) in all comparison operators. * Make sure, "other" is a compatible type (VersionInfo, dict, list, tuple, or string)
1 parent 14cd842 commit 4482f8c

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

semver.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ def compare(self, other):
375375
"""
376376
Compare self with other.
377377
378-
:param other: second version (can be string or a VersionInfo instance)
378+
:param other: the second version (can be string, a dict, tuple/list, or
379+
a VersionInfo instance)
379380
:return: The return value is negative if ver1 < ver2,
380381
zero if ver1 == ver2 and strictly positive if ver1 > ver2
381382
:rtype: int
@@ -386,10 +387,16 @@ def compare(self, other):
386387
1
387388
>>> semver.VersionInfo.parse("2.0.0").compare("2.0.0")
388389
0
390+
>>> semver.VersionInfo.parse("2.0.0").compare(dict(major=2, minor=0, patch=0))
391+
0
389392
"""
390393
cls = type(self)
391394
if isinstance(other, str):
392395
other = cls.parse(other)
396+
elif isinstance(other, dict):
397+
other = cls(**other)
398+
elif isinstance(other, (tuple, list)):
399+
other = cls(*other)
393400
elif not isinstance(other, cls):
394401
raise TypeError(
395402
"Expected str or {} instance, but got {}".format(
@@ -417,27 +424,27 @@ def compare(self, other):
417424

418425
@comparator
419426
def __eq__(self, other):
420-
return _compare_by_keys(self.to_dict(), _to_dict(other)) == 0
427+
return self.compare(other) == 0
421428

422429
@comparator
423430
def __ne__(self, other):
424-
return _compare_by_keys(self.to_dict(), _to_dict(other)) != 0
431+
return self.compare(other) != 0
425432

426433
@comparator
427434
def __lt__(self, other):
428-
return _compare_by_keys(self.to_dict(), _to_dict(other)) < 0
435+
return self.compare(other) < 0
429436

430437
@comparator
431438
def __le__(self, other):
432-
return _compare_by_keys(self.to_dict(), _to_dict(other)) <= 0
439+
return self.compare(other) <= 0
433440

434441
@comparator
435442
def __gt__(self, other):
436-
return _compare_by_keys(self.to_dict(), _to_dict(other)) > 0
443+
return self.compare(other) > 0
437444

438445
@comparator
439446
def __ge__(self, other):
440-
return _compare_by_keys(self.to_dict(), _to_dict(other)) >= 0
447+
return self.compare(other) >= 0
441448

442449
def __repr__(self):
443450
s = ", ".join("%s=%r" % (key, val) for key, val in self.to_dict().items())
@@ -651,25 +658,6 @@ def cmp_prerelease_tag(a, b):
651658
return cmp(len(a), len(b))
652659

653660

654-
def _compare_by_keys(d1, d2):
655-
for key in ["major", "minor", "patch"]:
656-
v = cmp(d1.get(key), d2.get(key))
657-
if v:
658-
return v
659-
660-
rc1, rc2 = d1.get("prerelease"), d2.get("prerelease")
661-
rccmp = _nat_cmp(rc1, rc2)
662-
663-
if not rccmp:
664-
return 0
665-
if not rc1:
666-
return 1
667-
elif not rc2:
668-
return -1
669-
670-
return rccmp
671-
672-
673661
@deprecated(version="2.10.0")
674662
def compare(ver1, ver2):
675663
"""
@@ -689,9 +677,7 @@ def compare(ver1, ver2):
689677
0
690678
"""
691679
v1 = VersionInfo.parse(ver1)
692-
# v2 = VersionInfo.parse(ver2)
693680
return v1.compare(ver2)
694-
# return _compare_by_keys(v1, v2)
695681

696682

697683
@deprecated(version="2.10.0")

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