diff --git a/pandas/io/clipboards.py b/pandas/io/clipboards.py index 5990acd7191db..0968f1facf128 100644 --- a/pandas/io/clipboards.py +++ b/pandas/io/clipboards.py @@ -1,4 +1,6 @@ """ io on the clipboard """ +from __future__ import annotations + from io import StringIO import warnings @@ -10,7 +12,7 @@ ) -def read_clipboard(sep=r"\s+", **kwargs): # pragma: no cover +def read_clipboard(sep: str = r"\s+", **kwargs): # pragma: no cover r""" Read text from clipboard and pass to read_csv. @@ -83,7 +85,9 @@ def read_clipboard(sep=r"\s+", **kwargs): # pragma: no cover return read_csv(StringIO(text), sep=sep, **kwargs) -def to_clipboard(obj, excel=True, sep=None, **kwargs): # pragma: no cover +def to_clipboard( + obj, excel: bool | None = True, sep: str | None = None, **kwargs +) -> None: # pragma: no cover """ Attempt to write text representation of object to the system clipboard The clipboard can be then pasted into Excel for example. diff --git a/pandas/io/common.py b/pandas/io/common.py index e54230c06d9b3..eaf6f6475ec84 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -174,7 +174,7 @@ def _expand_user(filepath_or_buffer: str | BaseBufferT) -> str | BaseBufferT: return filepath_or_buffer -def validate_header_arg(header) -> None: +def validate_header_arg(header: object) -> None: if isinstance(header, bool): raise TypeError( "Passing a bool to header is invalid. Use header=None for no header or " @@ -662,8 +662,7 @@ def get_handle( mode += "b" # validate encoding and errors - if isinstance(encoding, str): - codecs.lookup(encoding) + codecs.lookup(encoding) if isinstance(errors, str): codecs.lookup_error(errors) diff --git a/pandas/io/date_converters.py b/pandas/io/date_converters.py index ef60afa195234..077524fbee465 100644 --- a/pandas/io/date_converters.py +++ b/pandas/io/date_converters.py @@ -103,13 +103,13 @@ def generic_parser(parse_func, *cols): return results -def _maybe_cast(arr): +def _maybe_cast(arr: np.ndarray) -> np.ndarray: if not arr.dtype.type == np.object_: arr = np.array(arr, dtype=object) return arr -def _check_columns(cols): +def _check_columns(cols) -> int: if not len(cols): raise AssertionError("There must be at least 1 column") diff --git a/pandas/io/html.py b/pandas/io/html.py index 7985dcbec9672..05d7c2998ef27 100644 --- a/pandas/io/html.py +++ b/pandas/io/html.py @@ -12,6 +12,7 @@ from typing import ( Pattern, Sequence, + cast, ) from pandas._typing import ( @@ -47,7 +48,7 @@ _HAS_HTML5LIB = False -def _importers(): +def _importers() -> None: # import things we need # but make this done on a first use basis @@ -93,7 +94,7 @@ def _remove_whitespace(s: str, regex: Pattern = _RE_WHITESPACE) -> str: return regex.sub(" ", s.strip()) -def _get_skiprows(skiprows: int | Sequence[int] | slice | None): +def _get_skiprows(skiprows: int | Sequence[int] | slice | None) -> int | Sequence[int]: """ Get an iterator given an integer, slice or container. @@ -116,7 +117,7 @@ def _get_skiprows(skiprows: int | Sequence[int] | slice | None): start, step = skiprows.start or 0, skiprows.step or 1 return list(range(start, skiprows.stop, step)) elif isinstance(skiprows, numbers.Integral) or is_list_like(skiprows): - return skiprows + return cast("int | Sequence[int]", skiprows) elif skiprows is None: return 0 raise TypeError(f"{type(skiprows).__name__} is not a valid type for skipping rows") diff --git a/pandas/io/parquet.py b/pandas/io/parquet.py index f70649b29a1eb..4880c7730ff07 100644 --- a/pandas/io/parquet.py +++ b/pandas/io/parquet.py @@ -109,7 +109,7 @@ def _get_path_or_handle( class BaseImpl: @staticmethod - def validate_dataframe(df: DataFrame): + def validate_dataframe(df: DataFrame) -> None: if not isinstance(df, DataFrame): raise ValueError("to_parquet only supports IO with DataFrames") @@ -139,7 +139,7 @@ def validate_dataframe(df: DataFrame): def write(self, df: DataFrame, path, compression, **kwargs): raise AbstractMethodError(self) - def read(self, path, columns=None, **kwargs): + def read(self, path, columns=None, **kwargs) -> DataFrame: raise AbstractMethodError(self) @@ -164,7 +164,7 @@ def write( storage_options: StorageOptions = None, partition_cols: list[str] | None = None, **kwargs, - ): + ) -> None: self.validate_dataframe(df) from_pandas_kwargs: dict[str, Any] = {"schema": kwargs.pop("schema", None)} @@ -206,7 +206,7 @@ def read( use_nullable_dtypes=False, storage_options: StorageOptions = None, **kwargs, - ): + ) -> DataFrame: kwargs["use_pandas_metadata"] = True to_pandas_kwargs = {} @@ -266,7 +266,7 @@ def write( partition_cols=None, storage_options: StorageOptions = None, **kwargs, - ): + ) -> None: self.validate_dataframe(df) # thriftpy/protocol/compact.py:339: # DeprecationWarning: tostring() is deprecated. @@ -309,7 +309,7 @@ def write( def read( self, path, columns=None, storage_options: StorageOptions = None, **kwargs - ): + ) -> DataFrame: parquet_kwargs: dict[str, Any] = {} use_nullable_dtypes = kwargs.pop("use_nullable_dtypes", False) if Version(self.api.__version__) >= Version("0.7.1"): @@ -442,7 +442,7 @@ def read_parquet( storage_options: StorageOptions = None, use_nullable_dtypes: bool = False, **kwargs, -): +) -> DataFrame: """ Load a parquet object from the file path, returning a DataFrame. diff --git a/pandas/io/pickle.py b/pandas/io/pickle.py index 12156bf354919..2928d8c6520b0 100644 --- a/pandas/io/pickle.py +++ b/pandas/io/pickle.py @@ -30,7 +30,7 @@ def to_pickle( compression: CompressionOptions = "infer", protocol: int = pickle.HIGHEST_PROTOCOL, storage_options: StorageOptions = None, -): +) -> None: """ Pickle (serialize) object to file.