Add type annotations to _pytest._io.saferepr
This commit is contained in:
parent
9303de877a
commit
dc30d78845
|
@ -1,8 +1,9 @@
|
|||
import pprint
|
||||
import reprlib
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _format_repr_exception(exc, obj):
|
||||
def _format_repr_exception(exc: Exception, obj: Any) -> str:
|
||||
exc_name = type(exc).__name__
|
||||
try:
|
||||
exc_info = str(exc)
|
||||
|
@ -13,7 +14,7 @@ def _format_repr_exception(exc, obj):
|
|||
)
|
||||
|
||||
|
||||
def _ellipsize(s, maxsize):
|
||||
def _ellipsize(s: str, maxsize: int) -> str:
|
||||
if len(s) > maxsize:
|
||||
i = max(0, (maxsize - 3) // 2)
|
||||
j = max(0, maxsize - 3 - i)
|
||||
|
@ -26,19 +27,19 @@ class SafeRepr(reprlib.Repr):
|
|||
and includes information on exceptions raised during the call.
|
||||
"""
|
||||
|
||||
def __init__(self, maxsize):
|
||||
def __init__(self, maxsize: int) -> None:
|
||||
super().__init__()
|
||||
self.maxstring = maxsize
|
||||
self.maxsize = maxsize
|
||||
|
||||
def repr(self, x):
|
||||
def repr(self, x: Any) -> str:
|
||||
try:
|
||||
s = super().repr(x)
|
||||
except Exception as exc:
|
||||
s = _format_repr_exception(exc, x)
|
||||
return _ellipsize(s, self.maxsize)
|
||||
|
||||
def repr_instance(self, x, level):
|
||||
def repr_instance(self, x: Any, level: int) -> str:
|
||||
try:
|
||||
s = repr(x)
|
||||
except Exception as exc:
|
||||
|
@ -46,7 +47,7 @@ class SafeRepr(reprlib.Repr):
|
|||
return _ellipsize(s, self.maxsize)
|
||||
|
||||
|
||||
def safeformat(obj):
|
||||
def safeformat(obj: Any) -> str:
|
||||
"""return a pretty printed string for the given object.
|
||||
Failing __repr__ functions of user instances will be represented
|
||||
with a short exception info.
|
||||
|
@ -57,7 +58,7 @@ def safeformat(obj):
|
|||
return _format_repr_exception(exc, obj)
|
||||
|
||||
|
||||
def saferepr(obj, maxsize=240):
|
||||
def saferepr(obj: Any, maxsize: int = 240) -> str:
|
||||
"""return a size-limited safe repr-string for the given object.
|
||||
Failing __repr__ functions of user instances will be represented
|
||||
with a short exception info and 'saferepr' generally takes
|
||||
|
|
Loading…
Reference in New Issue