From dc30d78845970afd3089d4ff79b2df0eb86af156 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sun, 3 Nov 2019 15:23:37 +0200 Subject: [PATCH] Add type annotations to _pytest._io.saferepr --- src/_pytest/_io/saferepr.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/_pytest/_io/saferepr.py b/src/_pytest/_io/saferepr.py index 7704421a2..908fd2183 100644 --- a/src/_pytest/_io/saferepr.py +++ b/src/_pytest/_io/saferepr.py @@ -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