From 295312000338670c0d83dfb75ce16a34be851eb2 Mon Sep 17 00:00:00 2001 From: Benjamin Schubert Date: Fri, 17 Nov 2023 19:20:55 +0000 Subject: [PATCH] Fix typing information for the pprint module There is more type information that could be added. We can add those later to make it easier, this is jsut the minimum to allow linting to pass --- src/_pytest/_io/pprint.py | 19 ++++++++++++------- src/_pytest/_io/saferepr.py | 10 ++++------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/_pytest/_io/pprint.py b/src/_pytest/_io/pprint.py index 3bb4a2c7d..04f7edbbe 100644 --- a/src/_pytest/_io/pprint.py +++ b/src/_pytest/_io/pprint.py @@ -2,9 +2,6 @@ # (https://github.com/python/cpython/) at commit # c5140945c723ae6c4b7ee81ff720ac8ea4b52cfd (python3.12). # -# flake8: noqa -# type: ignore -# # # Original Author: Fred L. Drake, Jr. # fdrake@acm.org @@ -21,6 +18,11 @@ import re import sys as _sys import types as _types from io import StringIO as _StringIO +from typing import Any +from typing import Callable +from typing import Dict +from typing import IO +from typing import List class _safe_key: @@ -49,7 +51,7 @@ class _safe_key: def _safe_tuple(t): - "Helper function for comparing 2-tuples" + """Helper function for comparing 2-tuples""" return _safe_key(t[0]), _safe_key(t[1]) @@ -107,7 +109,7 @@ class PrettyPrinter: self._sort_dicts = sort_dicts self._underscore_numbers = underscore_numbers - def pformat(self, object): + def pformat(self, object: Any) -> str: sio = _StringIO() self._format(object, sio, 0, 0, {}, 0) return sio.getvalue() @@ -157,7 +159,10 @@ class PrettyPrinter: self._format_namespace_items(items, stream, indent, allowance, context, level) stream.write(")") - _dispatch = {} + _dispatch: Dict[ + Callable[..., str], + Callable[["PrettyPrinter", Any, IO[str], int, int, Dict[int, int], int], str], + ] = {} def _pprint_dict(self, object, stream, indent, allowance, context, level): write = stream.write @@ -544,7 +549,7 @@ class PrettyPrinter: context[objid] = 1 readable = True recursive = False - components = [] + components: List[str] = [] append = components.append level += 1 if self._sort_dicts: diff --git a/src/_pytest/_io/saferepr.py b/src/_pytest/_io/saferepr.py index 50d6a303a..ba8ea4302 100644 --- a/src/_pytest/_io/saferepr.py +++ b/src/_pytest/_io/saferepr.py @@ -5,7 +5,7 @@ from typing import Dict from typing import IO from typing import Optional -from .pprint import PrettyPrinter # type: ignore +from .pprint import PrettyPrinter def _try_repr_or_str(obj: object) -> str: @@ -148,13 +148,11 @@ class AlwaysDispatchingPrettyPrinter(PrettyPrinter): 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] + p = self._dispatch.get(type(object).__repr__, None) objid = id(object) if objid in context or p is None: - # Type ignored because _format is private. - super()._format( # type: ignore[misc] + super()._format( object, stream, indent, @@ -177,6 +175,6 @@ def _pformat_dispatch( *, compact: bool = False, ) -> str: - return AlwaysDispatchingPrettyPrinter( # type: ignore + return AlwaysDispatchingPrettyPrinter( indent=indent, width=width, depth=depth, compact=compact ).pformat(object)