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
This commit is contained in:
Benjamin Schubert 2023-11-17 19:20:55 +00:00
parent 5fae5ef73e
commit 2953120003
2 changed files with 16 additions and 13 deletions

View File

@ -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:

View File

@ -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)