From 018c051a8eeedcb779e650b7ac816a709ebb2460 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sat, 4 Apr 2020 15:17:56 +0800 Subject: [PATCH 1/2] Implement equality on candidate classes --- .../resolution/resolvelib/candidates.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/pip/_internal/resolution/resolvelib/candidates.py b/src/pip/_internal/resolution/resolvelib/candidates.py index 664c5858f..a3397a271 100644 --- a/src/pip/_internal/resolution/resolvelib/candidates.py +++ b/src/pip/_internal/resolution/resolvelib/candidates.py @@ -282,6 +282,17 @@ class AlreadyInstalledCandidate(Candidate): distribution=self.dist, ) + def __eq__(self, other): + # type: (Any) -> bool + if isinstance(other, self.__class__): + return self.name == other.name and self.version == other.version + return False + + # Needed for Python 2, which does not implement this by default + def __ne__(self, other): + # type: (Any) -> bool + return not self.__eq__(other) + @property def name(self): # type: () -> str @@ -345,6 +356,17 @@ class ExtrasCandidate(Candidate): extras=self.extras, ) + def __eq__(self, other): + # type: (Any) -> bool + if isinstance(other, self.__class__): + return self.base == other.base and self.extras == other.extras + return False + + # Needed for Python 2, which does not implement this by default + def __ne__(self, other): + # type: (Any) -> bool + return not self.__eq__(other) + @property def name(self): # type: () -> str From 591d476fca082e3fa92061aca514a53e5cb8ce06 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sat, 4 Apr 2020 21:51:20 +0800 Subject: [PATCH 2/2] Note on why we don't implement equality --- src/pip/_internal/resolution/resolvelib/candidates.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/pip/_internal/resolution/resolvelib/candidates.py b/src/pip/_internal/resolution/resolvelib/candidates.py index a3397a271..7bc4815b0 100644 --- a/src/pip/_internal/resolution/resolvelib/candidates.py +++ b/src/pip/_internal/resolution/resolvelib/candidates.py @@ -420,6 +420,10 @@ class RequiresPythonCandidate(Candidate): version_info = sys.version_info[:3] self._version = Version(".".join(str(c) for c in version_info)) + # We don't need to implement __eq__() and __ne__() since there is always + # only one RequiresPythonCandidate in a resolution, i.e. the host Python. + # The built-in object.__eq__() and object.__ne__() do exactly what we want. + @property def name(self): # type: () -> str