Skip to content

TYP: Return annotations for io/{formats,json} #47516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ def _repr_fits_horizontal_(self, ignore_width: bool = False) -> bool:

# used by repr_html under IPython notebook or scripts ignore terminal
# dims
if ignore_width or not console.in_interactive_session():
if ignore_width or width is None or not console.in_interactive_session():
return True

if get_option("display.width") is not None or console.in_ipython_frontend():
Expand Down
9 changes: 5 additions & 4 deletions pandas/io/formats/console.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""
Internal module for console introspection
"""
from __future__ import annotations

from shutil import get_terminal_size


def get_console_size():
def get_console_size() -> tuple[int | None, int | None]:
"""
Return console size as tuple = (width, height).

Expand Down Expand Up @@ -43,14 +44,14 @@ def get_console_size():
# Note if the User sets width/Height to None (auto-detection)
# and we're in a script (non-inter), this will return (None,None)
# caller needs to deal.
return (display_width or terminal_width, display_height or terminal_height)
return display_width or terminal_width, display_height or terminal_height


# ----------------------------------------------------------------------
# Detect our environment


def in_interactive_session():
def in_interactive_session() -> bool:
"""
Check if we're running in an interactive shell.

Expand All @@ -75,7 +76,7 @@ def check_main():
return check_main()


def in_ipython_frontend():
def in_ipython_frontend() -> bool:
"""
Check if we're inside an IPython zmq frontend.

Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import (
Callable,
Generator,
Iterator,
)
import warnings

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

def parse(self, declarations_str: str):
def parse(self, declarations_str: str) -> Iterator[tuple[str, str]]:
"""
Generates (prop, value) pairs from declarations.

Expand Down
6 changes: 3 additions & 3 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def build_xlstyle(self, props: Mapping[str, str]) -> dict[str, dict[str, str]]:

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

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

@property
def header_style(self):
def header_style(self) -> dict[str, dict[str, str | bool]]:
return {
"font": {"bold": True},
"borders": {
Expand Down Expand Up @@ -850,7 +850,7 @@ def write(
freeze_panes=None,
engine=None,
storage_options: StorageOptions = None,
):
) -> None:
"""
writer : path-like, file-like, or ExcelWriter object
File path or existing ExcelWriter
Expand Down
8 changes: 6 additions & 2 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Callable,
Hashable,
Iterable,
Iterator,
List,
Mapping,
Sequence,
Expand Down Expand Up @@ -1203,12 +1204,15 @@ def save_to_buffer(
with get_buffer(buf, encoding=encoding) as f:
f.write(string)
if buf is None:
return f.getvalue()
# error: "WriteBuffer[str]" has no attribute "getvalue"
return f.getvalue() # type: ignore[attr-defined]
return None


@contextmanager
def get_buffer(buf: FilePath | WriteBuffer[str] | None, encoding: str | None = None):
def get_buffer(
buf: FilePath | WriteBuffer[str] | None, encoding: str | None = None
) -> Iterator[WriteBuffer[str]] | Iterator[StringIO]:
"""
Context manager to open, yield and close buffer for filenames or Path-like
objects, otherwise yield buf unchanged.
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def render(self) -> list[str]:
return self.elements

@property
def should_show_dimensions(self):
def should_show_dimensions(self) -> bool:
return self.fmt.should_show_dimensions

@property
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/formats/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def dtypes(self) -> Iterable[Dtype]:
return [self.data.dtypes]

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

return _get_dataframe_dtype_counts(DataFrame(self.data))
Expand Down Expand Up @@ -1087,7 +1087,7 @@ def _fill_non_empty_info(self) -> None:
if self.display_memory_usage:
self.add_memory_usage_line()

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

@property
Expand Down
123 changes: 119 additions & 4 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Callable,
Hashable,
Sequence,
overload,
)
import warnings

Expand Down Expand Up @@ -591,6 +592,52 @@ def to_excel(
engine=engine,
)

@overload
def to_latex(
self,
buf: FilePath | WriteBuffer[str],
*,
column_format: str | None = ...,
position: str | None = ...,
position_float: str | None = ...,
hrules: bool | None = ...,
clines: str | None = ...,
label: str | None = ...,
caption: str | tuple | None = ...,
sparse_index: bool | None = ...,
sparse_columns: bool | None = ...,
multirow_align: str | None = ...,
multicol_align: str | None = ...,
siunitx: bool = ...,
environment: str | None = ...,
encoding: str | None = ...,
convert_css: bool = ...,
) -> None:
...

@overload
def to_latex(
self,
buf: None = ...,
*,
column_format: str | None = ...,
position: str | None = ...,
position_float: str | None = ...,
hrules: bool | None = ...,
clines: str | None = ...,
label: str | None = ...,
caption: str | tuple | None = ...,
sparse_index: bool | None = ...,
sparse_columns: bool | None = ...,
multirow_align: str | None = ...,
multicol_align: str | None = ...,
siunitx: bool = ...,
environment: str | None = ...,
encoding: str | None = ...,
convert_css: bool = ...,
) -> str:
...

def to_latex(
self,
buf: FilePath | WriteBuffer[str] | None = None,
Expand All @@ -610,7 +657,7 @@ def to_latex(
environment: str | None = None,
encoding: str | None = None,
convert_css: bool = False,
):
) -> str | None:
r"""
Write Styler to a file, buffer or string in LaTeX format.

Expand Down Expand Up @@ -1161,6 +1208,46 @@ def to_latex(
)
return save_to_buffer(latex, buf=buf, encoding=encoding)

@overload
def to_html(
self,
buf: FilePath | WriteBuffer[str],
*,
table_uuid: str | None = ...,
table_attributes: str | None = ...,
sparse_index: bool | None = ...,
sparse_columns: bool | None = ...,
bold_headers: bool = ...,
caption: str | None = ...,
max_rows: int | None = ...,
max_columns: int | None = ...,
encoding: str | None = ...,
doctype_html: bool = ...,
exclude_styles: bool = ...,
**kwargs,
) -> None:
...

@overload
def to_html(
self,
buf: None = ...,
*,
table_uuid: str | None = ...,
table_attributes: str | None = ...,
sparse_index: bool | None = ...,
sparse_columns: bool | None = ...,
bold_headers: bool = ...,
caption: str | None = ...,
max_rows: int | None = ...,
max_columns: int | None = ...,
encoding: str | None = ...,
doctype_html: bool = ...,
exclude_styles: bool = ...,
**kwargs,
) -> str:
...

@Substitution(buf=buf, encoding=encoding)
def to_html(
self,
Expand All @@ -1178,7 +1265,7 @@ def to_html(
doctype_html: bool = False,
exclude_styles: bool = False,
**kwargs,
):
) -> str | None:
"""
Write Styler to a file, buffer or string in HTML-CSS format.

Expand Down Expand Up @@ -1292,18 +1379,46 @@ def to_html(
html, buf=buf, encoding=(encoding if buf is not None else None)
)

@overload
def to_string(
self,
buf: FilePath | WriteBuffer[str],
*,
encoding=...,
sparse_index: bool | None = ...,
sparse_columns: bool | None = ...,
max_rows: int | None = ...,
max_columns: int | None = ...,
delimiter: str = ...,
) -> None:
...

@overload
def to_string(
self,
buf: None = ...,
*,
encoding=...,
sparse_index: bool | None = ...,
sparse_columns: bool | None = ...,
max_rows: int | None = ...,
max_columns: int | None = ...,
delimiter: str = ...,
) -> str:
...

@Substitution(buf=buf, encoding=encoding)
def to_string(
self,
buf=None,
buf: FilePath | WriteBuffer[str] | None = None,
*,
encoding=None,
sparse_index: bool | None = None,
sparse_columns: bool | None = None,
max_rows: int | None = None,
max_columns: int | None = None,
delimiter: str = " ",
):
) -> str | None:
"""
Write Styler to a file, buffer or string in text format.

Expand Down
Loading