chore(api): remove redundant __str__ method when __repr__ is defined

This commit is contained in:
Xuehai Pan 2023-03-20 13:56:05 +00:00
parent 0bc40840a4
commit 27b573804d
5 changed files with 8 additions and 22 deletions

View file

@ -661,7 +661,7 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me
self._ident = (self.index, self.uuid())
self._hash = None
def __str__(self) -> str:
def __repr__(self) -> str:
"""Return a string representation of the device."""
return '{}(index={}, name="{}", total_memory={})'.format(
self.__class__.__name__,
@ -670,8 +670,6 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me
self.memory_total_human(),
)
__repr__ = __str__
def __eq__(self, other: object) -> bool:
"""Test equality to other object."""
if not isinstance(other, Device):
@ -2281,7 +2279,7 @@ class CudaDevice(Device):
self._ident = ((self._cuda_index, self.index), self.uuid())
def __str__(self) -> str:
def __repr__(self) -> str:
"""Return a string representation of the CUDA device."""
return '{}(cuda_index={}, nvml_index={}, name="{}", total_memory={})'.format(
self.__class__.__name__,
@ -2291,8 +2289,6 @@ class CudaDevice(Device):
self.memory_total_human(),
)
__repr__ = __str__
def __reduce__(self) -> tuple[type[CudaDevice], tuple[int]]:
"""Return state information for pickling."""
return self.__class__, (self._cuda_index,)

View file

@ -238,7 +238,7 @@ class CUDAError(Exception):
obj.value = value
return obj
def __str__(self) -> str:
def __repr__(self) -> str:
"""Return a string representation of the error."""
# pylint: disable=no-member
try:
@ -256,8 +256,6 @@ class CUDAError(Exception):
except CUDAError:
return f'CUDA Error with code {self.value}.'
__repr__ = __str__
def __eq__(self, other: object) -> bool:
"""Test equality to other object."""
if not isinstance(other, CUDAError):

View file

@ -289,7 +289,7 @@ class cudaError(Exception):
obj.value = value
return obj
def __str__(self) -> str:
def __repr__(self) -> str:
"""Return a string representation of the error."""
# pylint: disable=no-member
try:
@ -307,8 +307,6 @@ class cudaError(Exception):
except cudaError:
return f'CUDA Error with code {self.value}.'
__repr__ = __str__
def __eq__(self, other: object) -> bool:
"""Test equality to other object."""
if not isinstance(other, cudaError):

View file

@ -229,11 +229,9 @@ class HostProcess(host.Process, metaclass=ABCMeta):
pass
self._super_gone = value
def __str__(self) -> str:
def __repr__(self) -> str:
"""Return a string representation of the process."""
return super().__str__().replace(self.__class__.__module__ + '.', '', 1)
__repr__ = __str__
return super().__repr__().replace(self.__class__.__module__ + '.', '', 1)
def __reduce__(self) -> tuple[type[HostProcess], tuple[int]]:
"""Return state information for pickling."""
@ -520,7 +518,7 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi
if not hasattr(self, f'_gpu_{util}_utilization'):
setattr(self, f'_gpu_{util}_utilization', NA)
def __str__(self) -> str:
def __repr__(self) -> str:
"""Return a string representation of the GPU process."""
return '{}(pid={}, gpu_memory={}, type={}, device={}, host={})'.format(
self.__class__.__name__,
@ -531,8 +529,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi
self.host,
)
__repr__ = __str__
def __eq__(self, other: object) -> bool:
"""Test equality to other object."""
if not isinstance(other, (GpuProcess, host.Process)):

View file

@ -624,7 +624,7 @@ class Snapshot:
for key, value in items.items():
setattr(self, key, value)
def __str__(self) -> str:
def __repr__(self) -> str:
"""Return a string representation of the snapshot."""
keys = set(self.__dict__.keys()).difference({'real', 'timestamp'})
keys = ['real', *sorted(keys)]
@ -641,8 +641,6 @@ class Snapshot:
',\n '.join(keyvals),
)
__repr__ = __str__
def __hash__(self) -> int:
"""Return a hash value of the snapshot."""
return hash((self.real, self.timestamp))