Skip to content

Commit 100d9eb

Browse files
committed
TYP: Return annotations for io/{formats,json}
1 parent 7d2f9b8 commit 100d9eb

File tree

10 files changed

+330
-35
lines changed

10 files changed

+330
-35
lines changed

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,8 @@ def _repr_fits_horizontal_(self, ignore_width: bool = False) -> bool:
10361036
value = buf.getvalue()
10371037
repr_width = max(len(line) for line in value.split("\n"))
10381038

1039-
return repr_width < width
1039+
# error: Unsupported operand types for < ("int" and "None")
1040+
return repr_width < width # type: ignore[operator]
10401041

10411042
def _info_repr(self) -> bool:
10421043
"""

pandas/io/formats/console.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""
22
Internal module for console introspection
33
"""
4+
from __future__ import annotations
45

56
from shutil import get_terminal_size
67

78

8-
def get_console_size():
9+
def get_console_size() -> tuple[int | None, int | None]:
910
"""
1011
Return console size as tuple = (width, height).
1112
@@ -43,14 +44,14 @@ def get_console_size():
4344
# Note if the User sets width/Height to None (auto-detection)
4445
# and we're in a script (non-inter), this will return (None,None)
4546
# caller needs to deal.
46-
return (display_width or terminal_width, display_height or terminal_height)
47+
return display_width or terminal_width, display_height or terminal_height
4748

4849

4950
# ----------------------------------------------------------------------
5051
# Detect our environment
5152

5253

53-
def in_interactive_session():
54+
def in_interactive_session() -> bool:
5455
"""
5556
Check if we're running in an interactive shell.
5657
@@ -75,7 +76,7 @@ def check_main():
7576
return check_main()
7677

7778

78-
def in_ipython_frontend():
79+
def in_ipython_frontend() -> bool:
7980
"""
8081
Check if we're inside an IPython zmq frontend.
8182

pandas/io/formats/css.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import (
88
Callable,
99
Generator,
10+
Iterator,
1011
)
1112
import warnings
1213

@@ -369,7 +370,7 @@ def atomize(self, declarations) -> Generator[tuple[str, str], None, None]:
369370
expand_margin = _side_expander("margin-{:s}")
370371
expand_padding = _side_expander("padding-{:s}")
371372

372-
def parse(self, declarations_str: str):
373+
def parse(self, declarations_str: str) -> Iterator[tuple[str, str]]:
373374
"""
374375
Generates (prop, value) pairs from declarations.
375376

pandas/io/formats/excel.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def build_xlstyle(self, props: Mapping[str, str]) -> dict[str, dict[str, str]]:
197197

198198
# TODO: handle cell width and height: needs support in pandas.io.excel
199199

200-
def remove_none(d: dict[str, str]) -> None:
200+
def remove_none(d: dict[str, str | None]) -> None:
201201
"""Remove key where value is None, through nested dicts"""
202202
for k, v in list(d.items()):
203203
if v is None:
@@ -528,7 +528,7 @@ def __init__(
528528
self.inf_rep = inf_rep
529529

530530
@property
531-
def header_style(self):
531+
def header_style(self) -> dict[str, dict[str, str | bool]]:
532532
return {
533533
"font": {"bold": True},
534534
"borders": {
@@ -850,7 +850,7 @@ def write(
850850
freeze_panes=None,
851851
engine=None,
852852
storage_options: StorageOptions = None,
853-
):
853+
) -> None:
854854
"""
855855
writer : path-like, file-like, or ExcelWriter object
856856
File path or existing ExcelWriter

pandas/io/formats/format.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
Callable,
2323
Hashable,
2424
Iterable,
25+
Iterator,
2526
List,
2627
Mapping,
2728
Sequence,
2829
cast,
30+
overload,
2931
)
3032
from unicodedata import east_asian_width
3133

@@ -1203,12 +1205,15 @@ def save_to_buffer(
12031205
with get_buffer(buf, encoding=encoding) as f:
12041206
f.write(string)
12051207
if buf is None:
1206-
return f.getvalue()
1208+
# error: "WriteBuffer[str]" has no attribute "getvalue"
1209+
return f.getvalue() # type: ignore[attr-defined]
12071210
return None
12081211

12091212

12101213
@contextmanager
1211-
def get_buffer(buf: FilePath | WriteBuffer[str] | None, encoding: str | None = None):
1214+
def get_buffer(
1215+
buf: FilePath | WriteBuffer[str] | None, encoding: str | None = None
1216+
) -> Iterator[WriteBuffer[str]] | Iterator[StringIO]:
12121217
"""
12131218
Context manager to open, yield and close buffer for filenames or Path-like
12141219
objects, otherwise yield buf unchanged.

pandas/io/formats/html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def render(self) -> list[str]:
8989
return self.elements
9090

9191
@property
92-
def should_show_dimensions(self):
92+
def should_show_dimensions(self) -> bool:
9393
return self.fmt.should_show_dimensions
9494

9595
@property

pandas/io/formats/info.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ def dtypes(self) -> Iterable[Dtype]:
566566
return [self.data.dtypes]
567567

568568
@property
569-
def dtype_counts(self):
569+
def dtype_counts(self) -> Mapping[str, int]:
570570
from pandas.core.frame import DataFrame
571571

572572
return _get_dataframe_dtype_counts(DataFrame(self.data))
@@ -1087,7 +1087,7 @@ def _fill_non_empty_info(self) -> None:
10871087
if self.display_memory_usage:
10881088
self.add_memory_usage_line()
10891089

1090-
def add_series_name_line(self):
1090+
def add_series_name_line(self) -> None:
10911091
self._lines.append(f"Series name: {self.data.name}")
10921092

10931093
@property

pandas/io/formats/style.py

+119-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Callable,
1313
Hashable,
1414
Sequence,
15+
overload,
1516
)
1617
import warnings
1718

@@ -591,6 +592,52 @@ def to_excel(
591592
engine=engine,
592593
)
593594

595+
@overload
596+
def to_latex(
597+
self,
598+
buf: FilePath | WriteBuffer[str],
599+
*,
600+
column_format: str | None = ...,
601+
position: str | None = ...,
602+
position_float: str | None = ...,
603+
hrules: bool | None = ...,
604+
clines: str | None = ...,
605+
label: str | None = ...,
606+
caption: str | tuple | None = ...,
607+
sparse_index: bool | None = ...,
608+
sparse_columns: bool | None = ...,
609+
multirow_align: str | None = ...,
610+
multicol_align: str | None = ...,
611+
siunitx: bool = ...,
612+
environment: str | None = ...,
613+
encoding: str | None = ...,
614+
convert_css: bool = ...,
615+
) -> None:
616+
...
617+
618+
@overload
619+
def to_latex(
620+
self,
621+
buf: None = ...,
622+
*,
623+
column_format: str | None = ...,
624+
position: str | None = ...,
625+
position_float: str | None = ...,
626+
hrules: bool | None = ...,
627+
clines: str | None = ...,
628+
label: str | None = ...,
629+
caption: str | tuple | None = ...,
630+
sparse_index: bool | None = ...,
631+
sparse_columns: bool | None = ...,
632+
multirow_align: str | None = ...,
633+
multicol_align: str | None = ...,
634+
siunitx: bool = ...,
635+
environment: str | None = ...,
636+
encoding: str | None = ...,
637+
convert_css: bool = ...,
638+
) -> str:
639+
...
640+
594641
def to_latex(
595642
self,
596643
buf: FilePath | WriteBuffer[str] | None = None,
@@ -610,7 +657,7 @@ def to_latex(
610657
environment: str | None = None,
611658
encoding: str | None = None,
612659
convert_css: bool = False,
613-
):
660+
) -> str | None:
614661
r"""
615662
Write Styler to a file, buffer or string in LaTeX format.
616663
@@ -1161,6 +1208,46 @@ def to_latex(
11611208
)
11621209
return save_to_buffer(latex, buf=buf, encoding=encoding)
11631210

1211+
@overload
1212+
def to_html(
1213+
self,
1214+
buf: FilePath | WriteBuffer[str],
1215+
*,
1216+
table_uuid: str | None = ...,
1217+
table_attributes: str | None = ...,
1218+
sparse_index: bool | None = ...,
1219+
sparse_columns: bool | None = ...,
1220+
bold_headers: bool = ...,
1221+
caption: str | None = ...,
1222+
max_rows: int | None = ...,
1223+
max_columns: int | None = ...,
1224+
encoding: str | None = ...,
1225+
doctype_html: bool = ...,
1226+
exclude_styles: bool = ...,
1227+
**kwargs,
1228+
) -> None:
1229+
...
1230+
1231+
@overload
1232+
def to_html(
1233+
self,
1234+
buf: None = ...,
1235+
*,
1236+
table_uuid: str | None = ...,
1237+
table_attributes: str | None = ...,
1238+
sparse_index: bool | None = ...,
1239+
sparse_columns: bool | None = ...,
1240+
bold_headers: bool = ...,
1241+
caption: str | None = ...,
1242+
max_rows: int | None = ...,
1243+
max_columns: int | None = ...,
1244+
encoding: str | None = ...,
1245+
doctype_html: bool = ...,
1246+
exclude_styles: bool = ...,
1247+
**kwargs,
1248+
) -> str:
1249+
...
1250+
11641251
@Substitution(buf=buf, encoding=encoding)
11651252
def to_html(
11661253
self,
@@ -1178,7 +1265,7 @@ def to_html(
11781265
doctype_html: bool = False,
11791266
exclude_styles: bool = False,
11801267
**kwargs,
1181-
):
1268+
) -> str | None:
11821269
"""
11831270
Write Styler to a file, buffer or string in HTML-CSS format.
11841271
@@ -1292,18 +1379,46 @@ def to_html(
12921379
html, buf=buf, encoding=(encoding if buf is not None else None)
12931380
)
12941381

1382+
@overload
1383+
def to_string(
1384+
self,
1385+
buf: FilePath | WriteBuffer[str],
1386+
*,
1387+
encoding=...,
1388+
sparse_index: bool | None = ...,
1389+
sparse_columns: bool | None = ...,
1390+
max_rows: int | None = ...,
1391+
max_columns: int | None = ...,
1392+
delimiter: str = ...,
1393+
) -> None:
1394+
...
1395+
1396+
@overload
1397+
def to_string(
1398+
self,
1399+
buf: None = ...,
1400+
*,
1401+
encoding=...,
1402+
sparse_index: bool | None = ...,
1403+
sparse_columns: bool | None = ...,
1404+
max_rows: int | None = ...,
1405+
max_columns: int | None = ...,
1406+
delimiter: str = ...,
1407+
) -> str:
1408+
...
1409+
12951410
@Substitution(buf=buf, encoding=encoding)
12961411
def to_string(
12971412
self,
1298-
buf=None,
1413+
buf: FilePath | WriteBuffer[str] | None = None,
12991414
*,
13001415
encoding=None,
13011416
sparse_index: bool | None = None,
13021417
sparse_columns: bool | None = None,
13031418
max_rows: int | None = None,
13041419
max_columns: int | None = None,
13051420
delimiter: str = " ",
1306-
):
1421+
) -> str | None:
13071422
"""
13081423
Write Styler to a file, buffer or string in text format.
13091424

0 commit comments

Comments
 (0)