From c0d31b5915055be20b8593170cddb85770b9ccc9 Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Mon, 15 Dec 2014 07:41:02 -0500 Subject: Define a __hash__ on the packaging.version.Version subclasses In Python 3.x a subclass will not inherent the __hash__ method from the parent classes if the subclass defines a __eq__ method. This means that without defining our own __hash__ the SetuptoolsVersion classes are unhashable. --- pkg_resources.py | 3 +++ setuptools/tests/test_resources.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/pkg_resources.py b/pkg_resources.py index 5df5a3e6..4c7924b7 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -82,6 +82,9 @@ packaging = setuptools._vendor.packaging class _SetuptoolsVersionMixin(object): + def __hash__(self): + return super(_SetuptoolsVersionMixin, self).__hash__() + def __lt__(self, other): if isinstance(other, tuple): return tuple(self) < other diff --git a/setuptools/tests/test_resources.py b/setuptools/tests/test_resources.py index 13f80aa4..1902fb2c 100644 --- a/setuptools/tests/test_resources.py +++ b/setuptools/tests/test_resources.py @@ -528,6 +528,16 @@ class ParseTests(TestCase): self.assertTrue(parse_version("3.0") != tuple(parse_version("2.0"))) self.assertFalse(parse_version("3.0") != tuple(parse_version("3.0"))) + def testVersionHashable(self): + """ + Ensure that our versions stay hashable even though we've subclassed + them and added some shim code to them. + """ + self.assertEqual( + hash(parse_version("1.0")), + hash(parse_version("1.0")), + ) + class ScriptHeaderTests(TestCase): non_ascii_exe = '/Users/José/bin/python' -- cgit v1.2.1