Skip to content

Commit 98c5cae

Browse files
committed
Optimize hashing and comparison of AlreadyInstalledCandidate
1 parent 7adf2d1 commit 98c5cae

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/pip/_internal/resolution/resolvelib/candidates.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from pip._internal.req.req_install import InstallRequirement
2121
from pip._internal.utils.direct_url_helpers import direct_url_from_link
2222
from pip._internal.utils.misc import normalize_version_info
23+
from pip._internal.utils.models import KeyBasedCompareMixin
2324

2425
from .base import Candidate, CandidateVersion, Requirement, format_name
2526

@@ -325,7 +326,7 @@ def _prepare_distribution(self) -> BaseDistribution:
325326
return self._factory.preparer.prepare_editable_requirement(self._ireq)
326327

327328

328-
class AlreadyInstalledCandidate(Candidate):
329+
class AlreadyInstalledCandidate(Candidate, KeyBasedCompareMixin):
329330
is_installed = True
330331
source_link = None
331332

@@ -347,20 +348,16 @@ def __init__(
347348
skip_reason = "already satisfied"
348349
factory.preparer.prepare_installed_requirement(self._ireq, skip_reason)
349350

351+
KeyBasedCompareMixin.__init__(
352+
self, key=(self.name, self.version), defining_class=self.__class__
353+
)
354+
350355
def __str__(self) -> str:
351356
return str(self.dist)
352357

353358
def __repr__(self) -> str:
354359
return f"{self.__class__.__name__}({self.dist!r})"
355360

356-
def __hash__(self) -> int:
357-
return hash((self.__class__, self.name, self.version))
358-
359-
def __eq__(self, other: Any) -> bool:
360-
if isinstance(other, self.__class__):
361-
return self.name == other.name and self.version == other.version
362-
return False
363-
364361
@property
365362
def project_name(self) -> NormalizedName:
366363
return self.dist.canonical_name

0 commit comments

Comments
 (0)