Skip to content

Commit 433f34b

Browse files
authored
Typing: Narrow down types of arguments (NDFrame) (#52756)
* update typing in NDFrame * lint code * lint code some more * added sql method typing * revert mode literals
1 parent 7ab653d commit 433f34b

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

pandas/_typing.py

+10
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,13 @@ def closed(self) -> bool:
390390
]
391391
AlignJoin = Literal["outer", "inner", "left", "right"]
392392
DtypeBackend = Literal["pyarrow", "numpy_nullable"]
393+
394+
OpenFileErrors = Literal[
395+
"strict",
396+
"ignore",
397+
"replace",
398+
"surrogateescape",
399+
"xmlcharrefreplace",
400+
"backslashreplace",
401+
"namereplace",
402+
]

pandas/core/generic.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
Manager,
6969
NaPosition,
7070
NDFrameT,
71+
OpenFileErrors,
7172
RandomState,
7273
Renamer,
7374
Scalar,
@@ -2566,7 +2567,7 @@ def to_hdf(
25662567
nan_rep=None,
25672568
dropna: bool_t | None = None,
25682569
data_columns: Literal[True] | list[str] | None = None,
2569-
errors: str = "strict",
2570+
errors: OpenFileErrors = "strict",
25702571
encoding: str = "UTF-8",
25712572
) -> None:
25722573
"""
@@ -2713,7 +2714,7 @@ def to_sql(
27132714
index_label: IndexLabel = None,
27142715
chunksize: int | None = None,
27152716
dtype: DtypeArg | None = None,
2716-
method: str | None = None,
2717+
method: Literal["multi"] | Callable | None = None,
27172718
) -> int | None:
27182719
"""
27192720
Write records stored in a DataFrame to a SQL database.
@@ -3559,7 +3560,7 @@ def to_csv(
35593560
doublequote: bool_t = ...,
35603561
escapechar: str | None = ...,
35613562
decimal: str = ...,
3562-
errors: str = ...,
3563+
errors: OpenFileErrors = ...,
35633564
storage_options: StorageOptions = ...,
35643565
) -> str:
35653566
...
@@ -3586,7 +3587,7 @@ def to_csv(
35863587
doublequote: bool_t = ...,
35873588
escapechar: str | None = ...,
35883589
decimal: str = ...,
3589-
errors: str = ...,
3590+
errors: OpenFileErrors = ...,
35903591
storage_options: StorageOptions = ...,
35913592
) -> None:
35923593
...
@@ -3617,7 +3618,7 @@ def to_csv(
36173618
doublequote: bool_t = True,
36183619
escapechar: str | None = None,
36193620
decimal: str = ".",
3620-
errors: str = "strict",
3621+
errors: OpenFileErrors = "strict",
36213622
storage_options: StorageOptions = None,
36223623
) -> str | None:
36233624
r"""

pandas/io/sql.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from typing import (
2424
TYPE_CHECKING,
2525
Any,
26+
Callable,
2627
Iterator,
2728
Literal,
2829
Mapping,
@@ -683,7 +684,7 @@ def to_sql(
683684
index_label: IndexLabel = None,
684685
chunksize: int | None = None,
685686
dtype: DtypeArg | None = None,
686-
method: str | None = None,
687+
method: Literal["multi"] | Callable | None = None,
687688
engine: str = "auto",
688689
**engine_kwargs,
689690
) -> int | None:
@@ -996,7 +997,9 @@ def insert_data(self) -> tuple[list[str], list[np.ndarray]]:
996997
return column_names, data_list
997998

998999
def insert(
999-
self, chunksize: int | None = None, method: str | None = None
1000+
self,
1001+
chunksize: int | None = None,
1002+
method: Literal["multi"] | Callable | None = None,
10001003
) -> int | None:
10011004
# set insert method
10021005
if method is None:
@@ -1402,7 +1405,7 @@ def to_sql(
14021405
schema=None,
14031406
chunksize: int | None = None,
14041407
dtype: DtypeArg | None = None,
1405-
method=None,
1408+
method: Literal["multi"] | Callable | None = None,
14061409
engine: str = "auto",
14071410
**engine_kwargs,
14081411
) -> int | None:
@@ -1863,7 +1866,7 @@ def to_sql(
18631866
schema: str | None = None,
18641867
chunksize: int | None = None,
18651868
dtype: DtypeArg | None = None,
1866-
method=None,
1869+
method: Literal["multi"] | Callable | None = None,
18671870
engine: str = "auto",
18681871
**engine_kwargs,
18691872
) -> int | None:
@@ -2318,7 +2321,7 @@ def to_sql(
23182321
schema=None,
23192322
chunksize: int | None = None,
23202323
dtype: DtypeArg | None = None,
2321-
method=None,
2324+
method: Literal["multi"] | Callable | None = None,
23222325
engine: str = "auto",
23232326
**engine_kwargs,
23242327
) -> int | None:

0 commit comments

Comments
 (0)