Skip to content

Commit

Permalink
Eagerly calculate strings and hashes for requirements and _InstallReq…
Browse files Browse the repository at this point in the history
…uirementBackedCandidate
  • Loading branch information
notatallshaw committed Apr 28, 2024
1 parent e884c00 commit 8c7495e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/pip/_internal/resolution/resolvelib/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def __init__(
self._name = name
self._version = version
self.dist = self._prepare()
self._hash = hash((self.__class__, self._link)) # for faster __hash__

def __str__(self) -> str:
return f"{self.name} {self.version}"
Expand All @@ -162,7 +163,7 @@ def __repr__(self) -> str:
return f"{self.__class__.__name__}({str(self._link)!r})"

def __hash__(self) -> int:
return hash((self.__class__, self._link))
return self._hash

def __eq__(self, other: Any) -> bool:
if isinstance(other, self.__class__):
Expand Down
15 changes: 10 additions & 5 deletions src/pip/_internal/resolution/resolvelib/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class SpecifierRequirement(Requirement):
def __init__(self, ireq: InstallRequirement) -> None:
assert ireq.link is None, "This is a link, not a specifier"
self._ireq = ireq
self._ireq_str = str(ireq) # for faster __eq__
self._ireq_str_hash = hash(self._ireq_str) # for faster __hash__
self._extras = frozenset(canonicalize_name(e) for e in self._ireq.extras)

def __str__(self) -> str:
Expand All @@ -62,10 +64,10 @@ def __repr__(self) -> str:
def __eq__(self, other: object) -> bool:
if not isinstance(other, SpecifierRequirement):
return NotImplemented
return str(self._ireq) == str(other._ireq)
return self._ireq_str == other._ireq_str

def __hash__(self) -> int:
return hash(str(self._ireq))
return self._ireq_str_hash

@property
def project_name(self) -> NormalizedName:
Expand Down Expand Up @@ -114,15 +116,17 @@ class SpecifierWithoutExtrasRequirement(SpecifierRequirement):
def __init__(self, ireq: InstallRequirement) -> None:
assert ireq.link is None, "This is a link, not a specifier"
self._ireq = install_req_drop_extras(ireq)
self._ireq_str = str(ireq) # for faster __eq__
self._ireq_str_hash = hash(self._ireq_str) # for faster __hash__
self._extras = frozenset(canonicalize_name(e) for e in self._ireq.extras)

def __eq__(self, other: object) -> bool:
if not isinstance(other, SpecifierWithoutExtrasRequirement):
return NotImplemented
return str(self._ireq) == str(other._ireq)
return self._ireq_str == other._ireq_str

def __hash__(self) -> int:
return hash(str(self._ireq))
return self._ireq_str_hash


class RequiresPythonRequirement(Requirement):
Expand All @@ -131,6 +135,7 @@ class RequiresPythonRequirement(Requirement):
def __init__(self, specifier: SpecifierSet, match: Candidate) -> None:
self.specifier = specifier
self._specifier_string = str(specifier) # for faster __eq__
self._hash = hash((self._specifier_string, match)) # for faster __hash__
self._candidate = match

def __str__(self) -> str:
Expand All @@ -140,7 +145,7 @@ def __repr__(self) -> str:
return f"{self.__class__.__name__}({str(self.specifier)!r})"

def __hash__(self) -> int:
return hash((self._specifier_string, self._candidate))
return self._hash

def __eq__(self, other: Any) -> bool:
if not isinstance(other, RequiresPythonRequirement):
Expand Down

0 comments on commit 8c7495e

Please sign in to comment.