From 9efafb186fd907b308be5e06223213476a6b7dce Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sat, 21 Nov 2020 21:53:37 +0800 Subject: [PATCH] Implement __str__ for debuggability --- .../resolution/resolvelib/candidates.py | 17 +++++++++++++++++ .../resolution/resolvelib/requirements.py | 10 +++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/pip/_internal/resolution/resolvelib/candidates.py b/src/pip/_internal/resolution/resolvelib/candidates.py index 65585fd36..1fc2ff479 100644 --- a/src/pip/_internal/resolution/resolvelib/candidates.py +++ b/src/pip/_internal/resolution/resolvelib/candidates.py @@ -143,6 +143,10 @@ class _InstallRequirementBackedCandidate(Candidate): self._version = version self._dist = None # type: Optional[Distribution] + def __str__(self): + # type: () -> str + return "{} {}".format(self.name, self.version) + def __repr__(self): # type: () -> str return "{class_name}({link!r})".format( @@ -359,6 +363,10 @@ class AlreadyInstalledCandidate(Candidate): skip_reason = "already satisfied" factory.preparer.prepare_installed_requirement(self._ireq, skip_reason) + def __str__(self): + # type: () -> str + return str(self.dist) + def __repr__(self): # type: () -> str return "{class_name}({distribution!r})".format( @@ -445,6 +453,11 @@ class ExtrasCandidate(Candidate): self.base = base self.extras = extras + def __str__(self): + # type: () -> str + name, rest = str(self.base).split(" ", 1) + return "{}[{}] {}".format(name, ",".join(self.extras), rest) + def __repr__(self): # type: () -> str return "{class_name}(base={base!r}, extras={extras!r})".format( @@ -554,6 +567,10 @@ class RequiresPythonCandidate(Candidate): # only one RequiresPythonCandidate in a resolution, i.e. the host Python. # The built-in object.__eq__() and object.__ne__() do exactly what we want. + def __str__(self): + # type: () -> str + return "Python {}".format(self._version) + @property def name(self): # type: () -> str diff --git a/src/pip/_internal/resolution/resolvelib/requirements.py b/src/pip/_internal/resolution/resolvelib/requirements.py index bc1061f43..25cddceaf 100644 --- a/src/pip/_internal/resolution/resolvelib/requirements.py +++ b/src/pip/_internal/resolution/resolvelib/requirements.py @@ -17,6 +17,10 @@ class ExplicitRequirement(Requirement): # type: (Candidate) -> None self.candidate = candidate + def __str__(self): + # type: () -> str + return str(self.candidate) + def __repr__(self): # type: () -> str return "{class_name}({candidate!r})".format( @@ -106,6 +110,10 @@ class RequiresPythonRequirement(Requirement): self.specifier = specifier self._candidate = match + def __str__(self): + # type: () -> str + return "Python {}".format(self.specifier) + def __repr__(self): # type: () -> str return "{class_name}({specifier!r})".format( @@ -120,7 +128,7 @@ class RequiresPythonRequirement(Requirement): def format_for_error(self): # type: () -> str - return "Python " + str(self.specifier) + return str(self) def get_candidate_lookup(self): # type: () -> CandidateLookup