Implement __str__ for debuggability

This commit is contained in:
Tzu-ping Chung 2020-11-21 21:53:37 +08:00
parent 0f6750c98c
commit 9efafb186f
2 changed files with 26 additions and 1 deletions

View File

@ -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

View File

@ -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