Skip to content

Commit 2953120

Browse files
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
1 parent 5fae5ef commit 2953120

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/_pytest/_io/pprint.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
# (https://github.com/python/cpython/) at commit
33
# c5140945c723ae6c4b7ee81ff720ac8ea4b52cfd (python3.12).
44
#
5-
# flake8: noqa
6-
# type: ignore
7-
#
85
#
96
# Original Author: Fred L. Drake, Jr.
107
@@ -21,6 +18,11 @@
2118
import sys as _sys
2219
import types as _types
2320
from io import StringIO as _StringIO
21+
from typing import Any
22+
from typing import Callable
23+
from typing import Dict
24+
from typing import IO
25+
from typing import List
2426

2527

2628
class _safe_key:
@@ -49,7 +51,7 @@ def __lt__(self, other):
4951

5052

5153
def _safe_tuple(t):
52-
"Helper function for comparing 2-tuples"
54+
"""Helper function for comparing 2-tuples"""
5355
return _safe_key(t[0]), _safe_key(t[1])
5456

5557

@@ -107,7 +109,7 @@ def __init__(
107109
self._sort_dicts = sort_dicts
108110
self._underscore_numbers = underscore_numbers
109111

110-
def pformat(self, object):
112+
def pformat(self, object: Any) -> str:
111113
sio = _StringIO()
112114
self._format(object, sio, 0, 0, {}, 0)
113115
return sio.getvalue()
@@ -157,7 +159,10 @@ def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
157159
self._format_namespace_items(items, stream, indent, allowance, context, level)
158160
stream.write(")")
159161

160-
_dispatch = {}
162+
_dispatch: Dict[
163+
Callable[..., str],
164+
Callable[["PrettyPrinter", Any, IO[str], int, int, Dict[int, int], int], str],
165+
] = {}
161166

162167
def _pprint_dict(self, object, stream, indent, allowance, context, level):
163168
write = stream.write
@@ -544,7 +549,7 @@ def _safe_repr(self, object, context, maxlevels, level):
544549
context[objid] = 1
545550
readable = True
546551
recursive = False
547-
components = []
552+
components: List[str] = []
548553
append = components.append
549554
level += 1
550555
if self._sort_dicts:

src/_pytest/_io/saferepr.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import IO
66
from typing import Optional
77

8-
from .pprint import PrettyPrinter # type: ignore
8+
from .pprint import PrettyPrinter
99

1010

1111
def _try_repr_or_str(obj: object) -> str:
@@ -148,13 +148,11 @@ def _format(
148148
context: Dict[int, Any],
149149
level: int,
150150
) -> None:
151-
# Type ignored because _dispatch is private.
152-
p = self._dispatch.get(type(object).__repr__, None) # type: ignore[attr-defined]
151+
p = self._dispatch.get(type(object).__repr__, None)
153152

154153
objid = id(object)
155154
if objid in context or p is None:
156-
# Type ignored because _format is private.
157-
super()._format( # type: ignore[misc]
155+
super()._format(
158156
object,
159157
stream,
160158
indent,
@@ -177,6 +175,6 @@ def _pformat_dispatch(
177175
*,
178176
compact: bool = False,
179177
) -> str:
180-
return AlwaysDispatchingPrettyPrinter( # type: ignore
178+
return AlwaysDispatchingPrettyPrinter(
181179
indent=indent, width=width, depth=depth, compact=compact
182180
).pformat(object)

0 commit comments

Comments
 (0)