Skip to content

Commit

Permalink
Typing: Narrow down types of arguments (NDFrame) (#52756)
Browse files Browse the repository at this point in the history
* update typing in NDFrame

* lint code

* lint code some more

* added sql method typing

* revert mode literals
  • Loading branch information
kevinanker authored Apr 19, 2023
1 parent 7ab653d commit 433f34b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
10 changes: 10 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,13 @@ def closed(self) -> bool:
]
AlignJoin = Literal["outer", "inner", "left", "right"]
DtypeBackend = Literal["pyarrow", "numpy_nullable"]

OpenFileErrors = Literal[
"strict",
"ignore",
"replace",
"surrogateescape",
"xmlcharrefreplace",
"backslashreplace",
"namereplace",
]
11 changes: 6 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
Manager,
NaPosition,
NDFrameT,
OpenFileErrors,
RandomState,
Renamer,
Scalar,
Expand Down Expand Up @@ -2566,7 +2567,7 @@ def to_hdf(
nan_rep=None,
dropna: bool_t | None = None,
data_columns: Literal[True] | list[str] | None = None,
errors: str = "strict",
errors: OpenFileErrors = "strict",
encoding: str = "UTF-8",
) -> None:
"""
Expand Down Expand Up @@ -2713,7 +2714,7 @@ def to_sql(
index_label: IndexLabel = None,
chunksize: int | None = None,
dtype: DtypeArg | None = None,
method: str | None = None,
method: Literal["multi"] | Callable | None = None,
) -> int | None:
"""
Write records stored in a DataFrame to a SQL database.
Expand Down Expand Up @@ -3559,7 +3560,7 @@ def to_csv(
doublequote: bool_t = ...,
escapechar: str | None = ...,
decimal: str = ...,
errors: str = ...,
errors: OpenFileErrors = ...,
storage_options: StorageOptions = ...,
) -> str:
...
Expand All @@ -3586,7 +3587,7 @@ def to_csv(
doublequote: bool_t = ...,
escapechar: str | None = ...,
decimal: str = ...,
errors: str = ...,
errors: OpenFileErrors = ...,
storage_options: StorageOptions = ...,
) -> None:
...
Expand Down Expand Up @@ -3617,7 +3618,7 @@ def to_csv(
doublequote: bool_t = True,
escapechar: str | None = None,
decimal: str = ".",
errors: str = "strict",
errors: OpenFileErrors = "strict",
storage_options: StorageOptions = None,
) -> str | None:
r"""
Expand Down
13 changes: 8 additions & 5 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import (
TYPE_CHECKING,
Any,
Callable,
Iterator,
Literal,
Mapping,
Expand Down Expand Up @@ -683,7 +684,7 @@ def to_sql(
index_label: IndexLabel = None,
chunksize: int | None = None,
dtype: DtypeArg | None = None,
method: str | None = None,
method: Literal["multi"] | Callable | None = None,
engine: str = "auto",
**engine_kwargs,
) -> int | None:
Expand Down Expand Up @@ -996,7 +997,9 @@ def insert_data(self) -> tuple[list[str], list[np.ndarray]]:
return column_names, data_list

def insert(
self, chunksize: int | None = None, method: str | None = None
self,
chunksize: int | None = None,
method: Literal["multi"] | Callable | None = None,
) -> int | None:
# set insert method
if method is None:
Expand Down Expand Up @@ -1402,7 +1405,7 @@ def to_sql(
schema=None,
chunksize: int | None = None,
dtype: DtypeArg | None = None,
method=None,
method: Literal["multi"] | Callable | None = None,
engine: str = "auto",
**engine_kwargs,
) -> int | None:
Expand Down Expand Up @@ -1863,7 +1866,7 @@ def to_sql(
schema: str | None = None,
chunksize: int | None = None,
dtype: DtypeArg | None = None,
method=None,
method: Literal["multi"] | Callable | None = None,
engine: str = "auto",
**engine_kwargs,
) -> int | None:
Expand Down Expand Up @@ -2318,7 +2321,7 @@ def to_sql(
schema=None,
chunksize: int | None = None,
dtype: DtypeArg | None = None,
method=None,
method: Literal["multi"] | Callable | None = None,
engine: str = "auto",
**engine_kwargs,
) -> int | None:
Expand Down

0 comments on commit 433f34b

Please sign in to comment.