Type annotate _pytest._io.saferepr

This commit is contained in:
Ran Benita 2020-05-01 14:40:15 +03:00
parent 247c4c0482
commit 30e3d473c4
2 changed files with 34 additions and 11 deletions

View File

@ -1,9 +1,12 @@
import pprint import pprint
import reprlib import reprlib
from typing import Any from typing import Any
from typing import Dict
from typing import IO
from typing import Optional
def _try_repr_or_str(obj): def _try_repr_or_str(obj: object) -> str:
try: try:
return repr(obj) return repr(obj)
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
@ -12,7 +15,7 @@ def _try_repr_or_str(obj):
return '{}("{}")'.format(type(obj).__name__, obj) return '{}("{}")'.format(type(obj).__name__, obj)
def _format_repr_exception(exc: BaseException, obj: Any) -> str: def _format_repr_exception(exc: BaseException, obj: object) -> str:
try: try:
exc_info = _try_repr_or_str(exc) exc_info = _try_repr_or_str(exc)
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
@ -42,7 +45,7 @@ class SafeRepr(reprlib.Repr):
self.maxstring = maxsize self.maxstring = maxsize
self.maxsize = maxsize self.maxsize = maxsize
def repr(self, x: Any) -> str: def repr(self, x: object) -> str:
try: try:
s = super().repr(x) s = super().repr(x)
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
@ -51,7 +54,7 @@ class SafeRepr(reprlib.Repr):
s = _format_repr_exception(exc, x) s = _format_repr_exception(exc, x)
return _ellipsize(s, self.maxsize) return _ellipsize(s, self.maxsize)
def repr_instance(self, x: Any, level: int) -> str: def repr_instance(self, x: object, level: int) -> str:
try: try:
s = repr(x) s = repr(x)
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
@ -61,7 +64,7 @@ class SafeRepr(reprlib.Repr):
return _ellipsize(s, self.maxsize) return _ellipsize(s, self.maxsize)
def safeformat(obj: Any) -> str: def safeformat(obj: object) -> str:
"""return a pretty printed string for the given object. """return a pretty printed string for the given object.
Failing __repr__ functions of user instances will be represented Failing __repr__ functions of user instances will be represented
with a short exception info. with a short exception info.
@ -72,7 +75,7 @@ def safeformat(obj: Any) -> str:
return _format_repr_exception(exc, obj) return _format_repr_exception(exc, obj)
def saferepr(obj: Any, maxsize: int = 240) -> str: def saferepr(obj: object, maxsize: int = 240) -> str:
"""return a size-limited safe repr-string for the given object. """return a size-limited safe repr-string for the given object.
Failing __repr__ functions of user instances will be represented Failing __repr__ functions of user instances will be represented
with a short exception info and 'saferepr' generally takes with a short exception info and 'saferepr' generally takes
@ -85,19 +88,39 @@ def saferepr(obj: Any, maxsize: int = 240) -> str:
class AlwaysDispatchingPrettyPrinter(pprint.PrettyPrinter): class AlwaysDispatchingPrettyPrinter(pprint.PrettyPrinter):
"""PrettyPrinter that always dispatches (regardless of width).""" """PrettyPrinter that always dispatches (regardless of width)."""
def _format(self, object, stream, indent, allowance, context, level): def _format(
p = self._dispatch.get(type(object).__repr__, None) self,
object: object,
stream: IO[str],
indent: int,
allowance: int,
context: Dict[int, Any],
level: int,
) -> None:
# Type ignored because _dispatch is private.
p = self._dispatch.get(type(object).__repr__, None) # type: ignore[attr-defined] # noqa: F821
objid = id(object) objid = id(object)
if objid in context or p is None: if objid in context or p is None:
return super()._format(object, stream, indent, allowance, context, level) # Type ignored because _format is private.
super()._format( # type: ignore[misc] # noqa: F821
object, stream, indent, allowance, context, level,
)
return
context[objid] = 1 context[objid] = 1
p(self, object, stream, indent, allowance, context, level + 1) p(self, object, stream, indent, allowance, context, level + 1)
del context[objid] del context[objid]
def _pformat_dispatch(object, indent=1, width=80, depth=None, *, compact=False): def _pformat_dispatch(
object: object,
indent: int = 1,
width: int = 80,
depth: Optional[int] = None,
*,
compact: bool = False
) -> str:
return AlwaysDispatchingPrettyPrinter( return AlwaysDispatchingPrettyPrinter(
indent=indent, width=width, depth=depth, compact=compact indent=indent, width=width, depth=depth, compact=compact
).pformat(object) ).pformat(object)

View File

@ -3,8 +3,8 @@ support for presenting detailed information in failing assertions.
""" """
import sys import sys
from typing import Any from typing import Any
from typing import List
from typing import Generator from typing import Generator
from typing import List
from typing import Optional from typing import Optional
from _pytest.assertion import rewrite from _pytest.assertion import rewrite