Skip to content

TYP: mostly missing return annotations in IO #44801

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
Dec 28, 2021
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
8 changes: 6 additions & 2 deletions pandas/io/clipboards.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
""" io on the clipboard """
from __future__ import annotations

from io import StringIO
import warnings

Expand All @@ -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.

Expand Down Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is # pragma: no cover for codecov? Does it need to be on the def to_clipboard-line?

"""
Attempt to write text representation of object to the system clipboard
The clipboard can be then pasted into Excel for example.
Expand Down
5 changes: 2 additions & 3 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be Any?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typeshed recommends to use Any when it is too difficult to create a type annotations. object should be used if literally any object can be accepted.

https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions

Note that Any is not the correct type to use if you want to indicate that some function can accept literally anything: in those cases use object instead.

if isinstance(header, bool):
raise TypeError(
"Passing a bool to header is invalid. Use header=None for no header or "
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions pandas/io/date_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
7 changes: 4 additions & 3 deletions pandas/io/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import (
Pattern,
Sequence,
cast,
)

from pandas._typing import (
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand All @@ -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")
Expand Down
14 changes: 7 additions & 7 deletions pandas/io/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)


Expand All @@ -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)}
Expand Down Expand Up @@ -206,7 +206,7 @@ def read(
use_nullable_dtypes=False,
storage_options: StorageOptions = None,
**kwargs,
):
) -> DataFrame:
kwargs["use_pandas_metadata"] = True

to_pandas_kwargs = {}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down