Skip to content

Commit 11836e5

Browse files
simonjayhawkinsproost
authored andcommitted
TYPING: more type hints for io.formats.printing (pandas-dev#27765)
1 parent 6837217 commit 11836e5

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

pandas/io/formats/printing.py

+16-24
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
"""
44

55
import sys
6-
from typing import Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
6+
from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
77

88
from pandas._config import get_option
99

1010
from pandas.core.dtypes.inference import is_sequence
1111

12+
EscapeChars = Union[Dict[str, str], Iterable[str]]
13+
1214

1315
def adjoin(space: int, *lists: List[str], **kwargs) -> str:
1416
"""
@@ -148,19 +150,16 @@ def _pprint_dict(
148150

149151

150152
def pprint_thing(
151-
thing,
153+
thing: Any,
152154
_nest_lvl: int = 0,
153-
escape_chars: Optional[Union[Dict[str, str], Iterable[str]]] = None,
155+
escape_chars: Optional[EscapeChars] = None,
154156
default_escapes: bool = False,
155157
quote_strings: bool = False,
156158
max_seq_items: Optional[int] = None,
157159
) -> str:
158160
"""
159161
This function is the sanctioned way of converting objects
160-
to a unicode representation.
161-
162-
properly handles nested sequences containing unicode strings
163-
(unicode(object) does not)
162+
to a string representation and properly handles nested sequences.
164163
165164
Parameters
166165
----------
@@ -178,21 +177,13 @@ def pprint_thing(
178177
179178
Returns
180179
-------
181-
result - unicode str
180+
str
182181
183182
"""
184183

185-
def as_escaped_unicode(thing, escape_chars=escape_chars):
186-
# Unicode is fine, else we try to decode using utf-8 and 'replace'
187-
# if that's not it either, we have no way of knowing and the user
188-
# should deal with it himself.
189-
190-
try:
191-
result = str(thing) # we should try this first
192-
except UnicodeDecodeError:
193-
# either utf-8 or we replace errors
194-
result = str(thing).decode("utf-8", "replace")
195-
184+
def as_escaped_string(
185+
thing: Any, escape_chars: Optional[EscapeChars] = escape_chars
186+
) -> str:
196187
translate = {"\t": r"\t", "\n": r"\n", "\r": r"\r"}
197188
if isinstance(escape_chars, dict):
198189
if default_escapes:
@@ -202,10 +193,11 @@ def as_escaped_unicode(thing, escape_chars=escape_chars):
202193
escape_chars = list(escape_chars.keys())
203194
else:
204195
escape_chars = escape_chars or tuple()
196+
197+
result = str(thing)
205198
for c in escape_chars:
206199
result = result.replace(c, translate[c])
207-
208-
return str(result)
200+
return result
209201

210202
if hasattr(thing, "__next__"):
211203
return str(thing)
@@ -224,11 +216,11 @@ def as_escaped_unicode(thing, escape_chars=escape_chars):
224216
max_seq_items=max_seq_items,
225217
)
226218
elif isinstance(thing, str) and quote_strings:
227-
result = "'{thing}'".format(thing=as_escaped_unicode(thing))
219+
result = "'{thing}'".format(thing=as_escaped_string(thing))
228220
else:
229-
result = as_escaped_unicode(thing)
221+
result = as_escaped_string(thing)
230222

231-
return str(result) # always unicode
223+
return result
232224

233225

234226
def pprint_thing_encoded(

0 commit comments

Comments
 (0)