-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix comparison #104
Fix comparison #104
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,7 @@ class VersionInfo(object): | |
""" | ||
__slots__ = ('_major', '_minor', '_patch', '_prerelease', '_build') | ||
|
||
def __init__(self, major, minor, patch, prerelease=None, build=None): | ||
def __init__(self, major, minor=0, patch=0, prerelease=None, build=None): | ||
self._major = major | ||
self._minor = minor | ||
self._patch = patch | ||
|
@@ -125,33 +125,27 @@ def _asdict(self): | |
)) | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, (VersionInfo, dict)): | ||
return NotImplemented | ||
_ensure_is_comparable(other) | ||
return _compare_by_keys(self._asdict(), _to_dict(other)) == 0 | ||
|
||
def __ne__(self, other): | ||
if not isinstance(other, (VersionInfo, dict)): | ||
return NotImplemented | ||
_ensure_is_comparable(other) | ||
return _compare_by_keys(self._asdict(), _to_dict(other)) != 0 | ||
|
||
def __lt__(self, other): | ||
if not isinstance(other, (VersionInfo, dict)): | ||
return NotImplemented | ||
_ensure_is_comparable(other) | ||
return _compare_by_keys(self._asdict(), _to_dict(other)) < 0 | ||
|
||
def __le__(self, other): | ||
if not isinstance(other, (VersionInfo, dict)): | ||
return NotImplemented | ||
_ensure_is_comparable(other) | ||
return _compare_by_keys(self._asdict(), _to_dict(other)) <= 0 | ||
|
||
def __gt__(self, other): | ||
if not isinstance(other, (VersionInfo, dict)): | ||
return NotImplemented | ||
_ensure_is_comparable(other) | ||
return _compare_by_keys(self._asdict(), _to_dict(other)) > 0 | ||
|
||
def __ge__(self, other): | ||
if not isinstance(other, (VersionInfo, dict)): | ||
return NotImplemented | ||
_ensure_is_comparable(other) | ||
return _compare_by_keys(self._asdict(), _to_dict(other)) >= 0 | ||
|
||
def __repr__(self): | ||
|
@@ -184,6 +178,8 @@ def parse(version): | |
def _to_dict(obj): | ||
if isinstance(obj, VersionInfo): | ||
return obj._asdict() | ||
elif isinstance(obj, tuple): | ||
return VersionInfo(*obj)._asdict() | ||
return obj | ||
|
||
|
||
|
@@ -261,6 +257,13 @@ def _compare_by_keys(d1, d2): | |
return rccmp | ||
|
||
|
||
def _ensure_is_comparable(other): | ||
comparable_types = (VersionInfo, dict, tuple) | ||
if not isinstance(other, comparable_types): | ||
raise NotImplementedError("other type %r must be in %r" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://docs.python.org/3/library/constants.html#NotImplemented You need to return NotImplemented here. Python itself will raise proper TypeError if objects aren't comparable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
% (type(other), comparable_types)) | ||
|
||
|
||
def compare(ver1, ver2): | ||
"""Compare two versions | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ppkt made a good point about using interfaces in place of dict/tuple. This will allow us to compare with dict-like / tuple-like objects for free (well, not really, but nothing to care about).
The only thing is
str
- it's Iterable, but unlikely*str
will give us what we really expected from it.