Skip to content

TYP: base_parser and readers #47359

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 1 commit into from
Jun 15, 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
15 changes: 15 additions & 0 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,21 @@ def get_handle(
...


@overload
def get_handle(
Copy link
Member

Choose a reason for hiding this comment

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

Just curious, why do we need this overloads when it's the same as the main function typing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mypy and pyright ignore annotations of the implementation when overloads are present (probably the expected behavior based on some PEP). The overloads cover "only" the cases when is_text True or False but not the case when it could be either of them.

The reason I fixed it is that pyright expects the ignore comment at a different line than mypy.

path_or_buf: FilePath | BaseBuffer,
mode: str,
*,
encoding: str | None = ...,
compression: CompressionOptions = ...,
memory_map: bool = ...,
is_text: bool = ...,
errors: str | None = ...,
storage_options: StorageOptions = ...,
) -> IOHandles[str] | IOHandles[bytes]:
...


@doc(compression_options=_shared_docs["compression_options"] % "path_or_buf")
def get_handle(
path_or_buf: FilePath | BaseBuffer,
Expand Down
5 changes: 3 additions & 2 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from pandas._typing import (
ArrayLike,
DtypeArg,
Scalar,
)
from pandas.errors import (
ParserError,
Expand Down Expand Up @@ -89,7 +90,7 @@ def __init__(self, kwds) -> None:

self.index_col = kwds.get("index_col", None)
self.unnamed_cols: set = set()
self.index_names: list | None = None
self.index_names: Sequence[Hashable] | None = None
self.col_names = None

self.parse_dates = _validate_parse_dates_arg(kwds.pop("parse_dates", False))
Expand Down Expand Up @@ -365,7 +366,7 @@ def _maybe_make_multi_index_columns(

@final
def _make_index(
self, data, alldata, columns, indexnamerow=False
self, data, alldata, columns, indexnamerow: list[Scalar] | None = None
) -> tuple[Index | None, Sequence[Hashable] | MultiIndex]:
index: Index | None
if not is_index_col(self.index_col) or not self.index_col:
Expand Down
27 changes: 12 additions & 15 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def read_csv(
comment: str | None = ...,
encoding: str | None = ...,
encoding_errors: str | None = ...,
dialect=...,
dialect: str | csv.Dialect | None = ...,
error_bad_lines: bool | None = ...,
warn_bad_lines: bool | None = ...,
on_bad_lines=...,
Expand Down Expand Up @@ -696,7 +696,7 @@ def read_csv(
comment: str | None = ...,
encoding: str | None = ...,
encoding_errors: str | None = ...,
dialect=...,
dialect: str | csv.Dialect | None = ...,
error_bad_lines: bool | None = ...,
warn_bad_lines: bool | None = ...,
on_bad_lines=...,
Expand Down Expand Up @@ -756,7 +756,7 @@ def read_csv(
comment: str | None = ...,
encoding: str | None = ...,
encoding_errors: str | None = ...,
dialect=...,
dialect: str | csv.Dialect | None = ...,
error_bad_lines: bool | None = ...,
warn_bad_lines: bool | None = ...,
on_bad_lines=...,
Expand Down Expand Up @@ -816,7 +816,7 @@ def read_csv(
comment: str | None = ...,
encoding: str | None = ...,
encoding_errors: str | None = ...,
dialect=...,
dialect: str | csv.Dialect | None = ...,
error_bad_lines: bool | None = ...,
warn_bad_lines: bool | None = ...,
on_bad_lines=...,
Expand Down Expand Up @@ -891,7 +891,7 @@ def read_csv(
comment: str | None = None,
encoding: str | None = None,
encoding_errors: str | None = "strict",
dialect=None,
dialect: str | csv.Dialect | None = None,
# Error Handling
error_bad_lines: bool | None = None,
warn_bad_lines: bool | None = None,
Expand Down Expand Up @@ -975,7 +975,7 @@ def read_table(
comment: str | None = ...,
encoding: str | None = ...,
encoding_errors: str | None = ...,
dialect=...,
dialect: str | csv.Dialect | None = ...,
error_bad_lines: bool | None = ...,
warn_bad_lines: bool | None = ...,
on_bad_lines=...,
Expand Down Expand Up @@ -1035,7 +1035,7 @@ def read_table(
comment: str | None = ...,
encoding: str | None = ...,
encoding_errors: str | None = ...,
dialect=...,
dialect: str | csv.Dialect | None = ...,
error_bad_lines: bool | None = ...,
warn_bad_lines: bool | None = ...,
on_bad_lines=...,
Expand Down Expand Up @@ -1095,7 +1095,7 @@ def read_table(
comment: str | None = ...,
encoding: str | None = ...,
encoding_errors: str | None = ...,
dialect=...,
dialect: str | csv.Dialect | None = ...,
error_bad_lines: bool | None = ...,
warn_bad_lines: bool | None = ...,
on_bad_lines=...,
Expand Down Expand Up @@ -1155,7 +1155,7 @@ def read_table(
comment: str | None = ...,
encoding: str | None = ...,
encoding_errors: str | None = ...,
dialect=...,
dialect: str | csv.Dialect | None = ...,
error_bad_lines: bool | None = ...,
warn_bad_lines: bool | None = ...,
on_bad_lines=...,
Expand Down Expand Up @@ -1230,7 +1230,7 @@ def read_table(
comment: str | None = None,
encoding: str | None = None,
encoding_errors: str | None = "strict",
dialect=None,
dialect: str | csv.Dialect | None = None,
# Error Handling
error_bad_lines: bool | None = None,
warn_bad_lines: bool | None = None,
Expand Down Expand Up @@ -1702,10 +1702,7 @@ def _make_engine(
if engine == "pyarrow":
is_text = False
mode = "rb"
# error: No overload variant of "get_handle" matches argument types
# "Union[str, PathLike[str], ReadCsvBuffer[bytes], ReadCsvBuffer[str]]"
# , "str", "bool", "Any", "Any", "Any", "Any", "Any"
self.handles = get_handle( # type: ignore[call-overload]
self.handles = get_handle(
f,
mode,
encoding=self.options.get("encoding", None),
Expand Down Expand Up @@ -1925,7 +1922,7 @@ def _stringify_na_values(na_values):


def _refine_defaults_read(
dialect: str | csv.Dialect,
dialect: str | csv.Dialect | None,
delimiter: str | None | lib.NoDefault,
delim_whitespace: bool,
engine: CSVEngine | None,
Expand Down
2 changes: 0 additions & 2 deletions pyright_reportGeneralTypeIssues.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@
"pandas/io/parsers/arrow_parser_wrapper.py",
"pandas/io/parsers/base_parser.py",
"pandas/io/parsers/c_parser_wrapper.py",
"pandas/io/parsers/python_parser.py",
"pandas/io/parsers/readers.py",
"pandas/io/pytables.py",
"pandas/io/sas/sas7bdat.py",
"pandas/io/sas/sasreader.py",
Expand Down