Skip to content

TYP: more return annotations for io/* #47524

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 2 commits into from
Jun 28, 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/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ class _BufferedWriter(BytesIO, ABC): # type: ignore[misc]
"""

@abstractmethod
def write_to_buffer(self):
def write_to_buffer(self) -> None:
...

def close(self) -> None:
Expand Down
13 changes: 9 additions & 4 deletions pandas/io/date_converters.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""This module is designed for community supported date conversion functions"""
from __future__ import annotations

import warnings

import numpy as np

from pandas._libs.tslibs import parsing
from pandas._typing import npt
from pandas.util._exceptions import find_stack_level


def parse_date_time(date_col, time_col):
def parse_date_time(date_col, time_col) -> npt.NDArray[np.object_]:
"""
Parse columns with dates and times into a single datetime column.

Expand All @@ -26,7 +29,7 @@ def parse_date_time(date_col, time_col):
return parsing.try_parse_date_and_time(date_col, time_col)


def parse_date_fields(year_col, month_col, day_col):
def parse_date_fields(year_col, month_col, day_col) -> npt.NDArray[np.object_]:
"""
Parse columns with years, months and days into a single date column.

Expand All @@ -48,7 +51,9 @@ def parse_date_fields(year_col, month_col, day_col):
return parsing.try_parse_year_month_day(year_col, month_col, day_col)


def parse_all_fields(year_col, month_col, day_col, hour_col, minute_col, second_col):
def parse_all_fields(
year_col, month_col, day_col, hour_col, minute_col, second_col
) -> npt.NDArray[np.object_]:
"""
Parse columns with datetime information into a single datetime column.

Expand Down Expand Up @@ -78,7 +83,7 @@ def parse_all_fields(year_col, month_col, day_col, hour_col, minute_col, second_
)


def generic_parser(parse_func, *cols):
def generic_parser(parse_func, *cols) -> np.ndarray:
"""
Use dateparser to parse columns with data information into a single datetime column.

Expand Down
18 changes: 9 additions & 9 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ class ExcelWriter(metaclass=abc.ABCMeta):
_supported_extensions: tuple[str, ...]

def __new__(
cls,
cls: type[ExcelWriter],
path: FilePath | WriteExcelBuffer | ExcelWriter,
engine: str | None = None,
date_format: str | None = None,
Expand All @@ -1079,7 +1079,7 @@ def __new__(
if_sheet_exists: Literal["error", "new", "replace", "overlay"] | None = None,
engine_kwargs: dict | None = None,
**kwargs,
):
) -> ExcelWriter:
if kwargs:
if engine_kwargs is not None:
raise ValueError("Cannot use both engine_kwargs and **kwargs")
Expand Down Expand Up @@ -1325,7 +1325,7 @@ def cur_sheet(self):
return self._cur_sheet

@property
def handles(self):
def handles(self) -> IOHandles[bytes]:
"""
Handles to Excel sheets.

Expand All @@ -1344,7 +1344,7 @@ def path(self):
self._deprecate("path")
return self._path

def __fspath__(self):
def __fspath__(self) -> str:
return getattr(self._handles.handle, "name", "")

def _get_sheet_name(self, sheet_name: str | None) -> str:
Expand Down Expand Up @@ -1402,10 +1402,10 @@ def check_extension(cls, ext: str) -> Literal[True]:
return True

# Allow use as a contextmanager
def __enter__(self):
def __enter__(self) -> ExcelWriter:
return self

def __exit__(self, exc_type, exc_value, traceback):
def __exit__(self, exc_type, exc_value, traceback) -> None:
self.close()

def close(self) -> None:
Expand Down Expand Up @@ -1699,13 +1699,13 @@ def close(self) -> None:
"""close io if necessary"""
self._reader.close()

def __enter__(self):
def __enter__(self) -> ExcelFile:
return self

def __exit__(self, exc_type, exc_value, traceback):
def __exit__(self, exc_type, exc_value, traceback) -> None:
self.close()

def __del__(self):
def __del__(self) -> None:
# Ensure we don't leak file descriptors, but put in try/except in case
# attributes are already deleted
try:
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

if TYPE_CHECKING:
from openpyxl.descriptors.serialisable import Serialisable
from openpyxl.workbook import Workbook


class OpenpyxlWriter(ExcelWriter):
Expand Down Expand Up @@ -79,7 +80,7 @@ def __init__(
self.book.remove(self.book.worksheets[0])

@property
def book(self):
def book(self) -> Workbook:
"""
Book instance of class openpyxl.workbook.Workbook.

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/feather_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def to_feather(
path: FilePath | WriteBuffer[bytes],
storage_options: StorageOptions = None,
**kwargs,
):
) -> None:
"""
Write a DataFrame to the binary Feather format.

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def _validate_parse_dates_presence(self, columns: Sequence[Hashable]) -> Iterabl
for col in cols_needed
]

def close(self):
def close(self) -> None:
pass

@final
Expand Down
6 changes: 5 additions & 1 deletion pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ def _exclude_implicit_index(
}, names

# legacy
def get_chunk(self, size=None):
def get_chunk(
self, size: int | None = None
) -> tuple[
Index | None, Sequence[Hashable] | MultiIndex, Mapping[Hashable, ArrayLike]
]:
if size is None:
# error: "PythonParser" has no attribute "chunksize"
size = self.chunksize # type: ignore[attr-defined]
Expand Down
36 changes: 26 additions & 10 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,22 @@ class _DeprecationConfig(NamedTuple):
}


def validate_integer(name, val, min_val=0):
@overload
def validate_integer(name, val: None, min_val=...) -> None:
...


@overload
def validate_integer(name, val: int | float, min_val=...) -> int:
...


@overload
def validate_integer(name, val: int | None, min_val=...) -> int | None:
...


def validate_integer(name, val: int | float | None, min_val=0) -> int | None:
"""
Checks whether the 'name' parameter for parsing is either
an integer OR float that can SAFELY be cast to an integer
Expand All @@ -509,17 +524,18 @@ def validate_integer(name, val, min_val=0):
min_val : int
Minimum allowed value (val < min_val will result in a ValueError)
"""
msg = f"'{name:s}' must be an integer >={min_val:d}"
if val is None:
return val

if val is not None:
if is_float(val):
if int(val) != val:
raise ValueError(msg)
val = int(val)
elif not (is_integer(val) and val >= min_val):
msg = f"'{name:s}' must be an integer >={min_val:d}"
if is_float(val):
if int(val) != val:
raise ValueError(msg)
val = int(val)
elif not (is_integer(val) and val >= min_val):
raise ValueError(msg)

return val
return int(val)


def _validate_names(names: Sequence[Hashable] | None) -> None:
Expand Down Expand Up @@ -1784,7 +1800,7 @@ def __exit__(self, exc_type, exc_value, traceback) -> None:
self.close()


def TextParser(*args, **kwds):
def TextParser(*args, **kwds) -> TextFileReader:
"""
Converts lists of lists/tuples into DataFrames with proper type inference
and optional (e.g. string to datetime) conversion. Also enables iterating
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/sas/sas_xport.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def _read_header(self):
dtype = np.dtype(dtypel)
self._dtype = dtype

def __next__(self):
def __next__(self) -> pd.DataFrame:
return self.read(nrows=self._chunksize or 1)

def _record_count(self) -> int:
Expand Down Expand Up @@ -434,7 +434,7 @@ def _record_count(self) -> int:

return (total_records_length - tail_pad) // self.record_length

def get_chunk(self, size=None):
def get_chunk(self, size=None) -> pd.DataFrame:
"""
Reads lines from Xport file and returns as dataframe

Expand Down
Loading