Skip to content

Commit e69a051

Browse files
authored
TYP: misc IO return types (#57228)
* TYP: misc IO return types * isort
1 parent cb16bb0 commit e69a051

File tree

17 files changed

+91
-45
lines changed

17 files changed

+91
-45
lines changed

pandas/_typing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,6 @@ def closed(self) -> bool:
529529
Callable[[HashableT], bool],
530530
None,
531531
]
532+
533+
# maintaine the sub-type of any hashable sequence
534+
SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable])

pandas/io/excel/_openpyxl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
if TYPE_CHECKING:
2727
from openpyxl import Workbook
2828
from openpyxl.descriptors.serialisable import Serialisable
29+
from openpyxl.styles import Fill
2930

3031
from pandas._typing import (
3132
ExcelWriterIfSheetExists,
@@ -244,7 +245,7 @@ def _convert_to_stop(cls, stop_seq):
244245
return map(cls._convert_to_color, stop_seq)
245246

246247
@classmethod
247-
def _convert_to_fill(cls, fill_dict: dict[str, Any]):
248+
def _convert_to_fill(cls, fill_dict: dict[str, Any]) -> Fill:
248249
"""
249250
Convert ``fill_dict`` to an openpyxl v2 Fill object.
250251

pandas/io/formats/excel.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ def build_border(
284284
for side in ["top", "right", "bottom", "left"]
285285
}
286286

287-
def _border_style(self, style: str | None, width: str | None, color: str | None):
287+
def _border_style(
288+
self, style: str | None, width: str | None, color: str | None
289+
) -> str | None:
288290
# convert styles and widths to openxml, one of:
289291
# 'dashDot'
290292
# 'dashDotDot'

pandas/io/formats/format.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,9 @@ def get_result_as_array(self) -> np.ndarray:
13461346
the parameters given at initialisation, as a numpy array
13471347
"""
13481348

1349-
def format_with_na_rep(values: ArrayLike, formatter: Callable, na_rep: str):
1349+
def format_with_na_rep(
1350+
values: ArrayLike, formatter: Callable, na_rep: str
1351+
) -> np.ndarray:
13501352
mask = isna(values)
13511353
formatted = np.array(
13521354
[
@@ -1358,7 +1360,7 @@ def format_with_na_rep(values: ArrayLike, formatter: Callable, na_rep: str):
13581360

13591361
def format_complex_with_na_rep(
13601362
values: ArrayLike, formatter: Callable, na_rep: str
1361-
):
1363+
) -> np.ndarray:
13621364
real_values = np.real(values).ravel() # type: ignore[arg-type]
13631365
imag_values = np.imag(values).ravel() # type: ignore[arg-type]
13641366
real_mask, imag_mask = isna(real_values), isna(imag_values)

pandas/io/formats/info.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def dtype_counts(self) -> Mapping[str, int]:
392392

393393
@property
394394
@abstractmethod
395-
def non_null_counts(self) -> Sequence[int]:
395+
def non_null_counts(self) -> list[int] | Series:
396396
"""Sequence of non-null counts for all columns or column (if series)."""
397397

398398
@property
@@ -486,7 +486,7 @@ def col_count(self) -> int:
486486
return len(self.ids)
487487

488488
@property
489-
def non_null_counts(self) -> Sequence[int]:
489+
def non_null_counts(self) -> Series:
490490
"""Sequence of non-null counts for all columns or column (if series)."""
491491
return self.data.count()
492492

@@ -546,7 +546,7 @@ def render(
546546
printer.to_buffer(buf)
547547

548548
@property
549-
def non_null_counts(self) -> Sequence[int]:
549+
def non_null_counts(self) -> list[int]:
550550
return [self.data.count()]
551551

552552
@property
@@ -750,7 +750,7 @@ def memory_usage_string(self) -> str:
750750
return self.info.memory_usage_string
751751

752752
@property
753-
def non_null_counts(self) -> Sequence[int]:
753+
def non_null_counts(self) -> list[int] | Series:
754754
return self.info.non_null_counts
755755

756756
def add_object_type_line(self) -> None:

pandas/io/formats/style.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3827,7 +3827,7 @@ def _background_gradient(
38273827
vmax: float | None = None,
38283828
gmap: Sequence | np.ndarray | DataFrame | Series | None = None,
38293829
text_only: bool = False,
3830-
):
3830+
) -> list[str] | DataFrame:
38313831
"""
38323832
Color background in a range according to the data or a gradient map
38333833
"""

pandas/io/formats/style_render.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,9 @@ def _class_styles(self):
20302030
}
20312031
]
20322032

2033-
def _pseudo_css(self, uuid: str, name: str, row: int, col: int, text: str):
2033+
def _pseudo_css(
2034+
self, uuid: str, name: str, row: int, col: int, text: str
2035+
) -> list[CSSDict]:
20342036
"""
20352037
For every table data-cell that has a valid tooltip (not None, NaN or
20362038
empty string) must create two pseudo CSS entries for the specific

pandas/io/html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def row_is_all_th(row):
469469

470470
def _expand_colspan_rowspan(
471471
self, rows, section: Literal["header", "footer", "body"]
472-
):
472+
) -> list[list]:
473473
"""
474474
Given a list of <tr>s, return a list of text rows.
475475

pandas/io/json/_normalize.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
TYPE_CHECKING,
1212
Any,
1313
DefaultDict,
14+
overload,
1415
)
1516

1617
import numpy as np
@@ -42,13 +43,35 @@ def convert_to_line_delimits(s: str) -> str:
4243
return convert_json_to_lines(s)
4344

4445

46+
@overload
4547
def nested_to_record(
46-
ds,
48+
ds: dict,
49+
prefix: str = ...,
50+
sep: str = ...,
51+
level: int = ...,
52+
max_level: int | None = ...,
53+
) -> dict[str, Any]:
54+
...
55+
56+
57+
@overload
58+
def nested_to_record(
59+
ds: list[dict],
60+
prefix: str = ...,
61+
sep: str = ...,
62+
level: int = ...,
63+
max_level: int | None = ...,
64+
) -> list[dict[str, Any]]:
65+
...
66+
67+
68+
def nested_to_record(
69+
ds: dict | list[dict],
4770
prefix: str = "",
4871
sep: str = ".",
4972
level: int = 0,
5073
max_level: int | None = None,
51-
):
74+
) -> dict[str, Any] | list[dict[str, Any]]:
5275
"""
5376
A simplified json_normalize
5477

pandas/io/parquet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def validate_dataframe(df: DataFrame) -> None:
150150
if not isinstance(df, DataFrame):
151151
raise ValueError("to_parquet only supports IO with DataFrames")
152152

153-
def write(self, df: DataFrame, path, compression, **kwargs):
153+
def write(self, df: DataFrame, path, compression, **kwargs) -> None:
154154
raise AbstractMethodError(self)
155155

156156
def read(self, path, columns=None, **kwargs) -> DataFrame:

0 commit comments

Comments
 (0)