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:
parent
5fae5ef73e
commit
2953120003
|
@ -2,9 +2,6 @@
|
||||||
# (https://github.com/python/cpython/) at commit
|
# (https://github.com/python/cpython/) at commit
|
||||||
# c5140945c723ae6c4b7ee81ff720ac8ea4b52cfd (python3.12).
|
# c5140945c723ae6c4b7ee81ff720ac8ea4b52cfd (python3.12).
|
||||||
#
|
#
|
||||||
# flake8: noqa
|
|
||||||
# type: ignore
|
|
||||||
#
|
|
||||||
#
|
#
|
||||||
# Original Author: Fred L. Drake, Jr.
|
# Original Author: Fred L. Drake, Jr.
|
||||||
# fdrake@acm.org
|
# fdrake@acm.org
|
||||||
|
@ -21,6 +18,11 @@ import re
|
||||||
import sys as _sys
|
import sys as _sys
|
||||||
import types as _types
|
import types as _types
|
||||||
from io import StringIO as _StringIO
|
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:
|
class _safe_key:
|
||||||
|
@ -49,7 +51,7 @@ class _safe_key:
|
||||||
|
|
||||||
|
|
||||||
def _safe_tuple(t):
|
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])
|
return _safe_key(t[0]), _safe_key(t[1])
|
||||||
|
|
||||||
|
|
||||||
|
@ -107,7 +109,7 @@ class PrettyPrinter:
|
||||||
self._sort_dicts = sort_dicts
|
self._sort_dicts = sort_dicts
|
||||||
self._underscore_numbers = underscore_numbers
|
self._underscore_numbers = underscore_numbers
|
||||||
|
|
||||||
def pformat(self, object):
|
def pformat(self, object: Any) -> str:
|
||||||
sio = _StringIO()
|
sio = _StringIO()
|
||||||
self._format(object, sio, 0, 0, {}, 0)
|
self._format(object, sio, 0, 0, {}, 0)
|
||||||
return sio.getvalue()
|
return sio.getvalue()
|
||||||
|
@ -157,7 +159,10 @@ class PrettyPrinter:
|
||||||
self._format_namespace_items(items, stream, indent, allowance, context, level)
|
self._format_namespace_items(items, stream, indent, allowance, context, level)
|
||||||
stream.write(")")
|
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):
|
def _pprint_dict(self, object, stream, indent, allowance, context, level):
|
||||||
write = stream.write
|
write = stream.write
|
||||||
|
@ -544,7 +549,7 @@ class PrettyPrinter:
|
||||||
context[objid] = 1
|
context[objid] = 1
|
||||||
readable = True
|
readable = True
|
||||||
recursive = False
|
recursive = False
|
||||||
components = []
|
components: List[str] = []
|
||||||
append = components.append
|
append = components.append
|
||||||
level += 1
|
level += 1
|
||||||
if self._sort_dicts:
|
if self._sort_dicts:
|
||||||
|
|
|
@ -5,7 +5,7 @@ from typing import Dict
|
||||||
from typing import IO
|
from typing import IO
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from .pprint import PrettyPrinter # type: ignore
|
from .pprint import PrettyPrinter
|
||||||
|
|
||||||
|
|
||||||
def _try_repr_or_str(obj: object) -> str:
|
def _try_repr_or_str(obj: object) -> str:
|
||||||
|
@ -148,13 +148,11 @@ class AlwaysDispatchingPrettyPrinter(PrettyPrinter):
|
||||||
context: Dict[int, Any],
|
context: Dict[int, Any],
|
||||||
level: int,
|
level: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
# Type ignored because _dispatch is private.
|
p = self._dispatch.get(type(object).__repr__, None)
|
||||||
p = self._dispatch.get(type(object).__repr__, None) # type: ignore[attr-defined]
|
|
||||||
|
|
||||||
objid = id(object)
|
objid = id(object)
|
||||||
if objid in context or p is None:
|
if objid in context or p is None:
|
||||||
# Type ignored because _format is private.
|
super()._format(
|
||||||
super()._format( # type: ignore[misc]
|
|
||||||
object,
|
object,
|
||||||
stream,
|
stream,
|
||||||
indent,
|
indent,
|
||||||
|
@ -177,6 +175,6 @@ def _pformat_dispatch(
|
||||||
*,
|
*,
|
||||||
compact: bool = False,
|
compact: bool = False,
|
||||||
) -> str:
|
) -> str:
|
||||||
return AlwaysDispatchingPrettyPrinter( # type: ignore
|
return AlwaysDispatchingPrettyPrinter(
|
||||||
indent=indent, width=width, depth=depth, compact=compact
|
indent=indent, width=width, depth=depth, compact=compact
|
||||||
).pformat(object)
|
).pformat(object)
|
||||||
|
|
Loading…
Reference in New Issue