Skip to content

Commit

Permalink
Canonicalize version before comparing specifiers (#283)
Browse files Browse the repository at this point in the history
* Add failing tests

* Canonicalize version before comparing specifiers

* Update changelog
  • Loading branch information
di committed Mar 26, 2020
1 parent d3f305d commit 20a9a33
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog
*unreleased*
~~~~~~~~~~~~

No unreleased changes.
* Canonicalize version before comparing specifiers. (:issue:`282`)

20.3 - 2020-03-05
~~~~~~~~~~~~~~~~~
Expand Down
6 changes: 4 additions & 2 deletions packaging/specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ._compat import string_types, with_metaclass
from ._typing import MYPY_CHECK_RUNNING
from .utils import canonicalize_version
from .version import Version, LegacyVersion, parse

if MYPY_CHECK_RUNNING: # pragma: no cover
Expand Down Expand Up @@ -134,7 +135,8 @@ def __str__(self):

def __hash__(self):
# type: () -> int
return hash(self._spec)
operator, version = self._spec
return hash((operator, canonicalize_version(version)))

def __eq__(self, other):
# type: (object) -> bool
Expand All @@ -146,7 +148,7 @@ def __eq__(self, other):
elif not isinstance(other, self.__class__):
return NotImplemented

return self._spec == other._spec
return hash(self) == hash(other)

def __ne__(self, other):
# type: (object) -> bool
Expand Down
12 changes: 12 additions & 0 deletions tests/test_specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ def test_comparison_true(self, left, right, op):
assert op(left, Specifier(right))
assert op(Specifier(left), right)

@pytest.mark.parametrize(("left", "right"), [("==2.8.0", "==2.8")])
def test_comparison_canonicalizes(self, left, right):
assert Specifier(left) == Specifier(right)
assert left == Specifier(right)
assert Specifier(left) == right

@pytest.mark.parametrize(
("left", "right", "op"),
itertools.chain(
Expand Down Expand Up @@ -961,6 +967,12 @@ def test_comparison_false(self, left, right, op):
assert not op(left, SpecifierSet(right))
assert not op(SpecifierSet(left), right)

@pytest.mark.parametrize(("left", "right"), [("==2.8.0", "==2.8")])
def test_comparison_canonicalizes(self, left, right):
assert SpecifierSet(left) == SpecifierSet(right)
assert left == SpecifierSet(right)
assert SpecifierSet(left) == right

def test_comparison_non_specifier(self):
assert SpecifierSet("==1.0") != 12
assert not SpecifierSet("==1.0") == 12

0 comments on commit 20a9a33

Please sign in to comment.