From 3bf6fd7bd2df6569bddf3ccd583b7f472473fe3f Mon Sep 17 00:00:00 2001 From: Kevin Anker <34544392+kjanker@users.noreply.github.com> Date: Tue, 18 Apr 2023 15:14:04 +0000 Subject: [PATCH 1/5] update typing in NDFrame --- pandas/_typing.py | 2 ++ pandas/core/generic.py | 33 +++++++++++++++++---------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index de02a549856ab..a9ed7ca76a49d 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -390,3 +390,5 @@ def closed(self) -> bool: ] AlignJoin = Literal["outer", "inner", "left", "right"] DtypeBackend = Literal["pyarrow", "numpy_nullable"] + +OpenFileErrors = Literal["strict", "ignore", "replace", "surrogateescape", "xmlcharrefreplace", "backslashreplace", "namereplace"] diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 582a043a8a78a..0d1a81e08f61c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -68,6 +68,7 @@ Manager, NaPosition, NDFrameT, + OpenFileErrors, RandomState, Renamer, Scalar, @@ -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: """ @@ -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. @@ -3548,7 +3549,7 @@ def to_csv( header: bool_t | list[str] = ..., index: bool_t = ..., index_label: IndexLabel | None = ..., - mode: str = ..., + mode: Literal["w", "x", "a"] = ..., encoding: str | None = ..., compression: CompressionOptions = ..., quoting: int | None = ..., @@ -3559,7 +3560,7 @@ def to_csv( doublequote: bool_t = ..., escapechar: str | None = ..., decimal: str = ..., - errors: str = ..., + errors: OpenFileErrors = ..., storage_options: StorageOptions = ..., ) -> str: ... @@ -3575,7 +3576,7 @@ def to_csv( header: bool_t | list[str] = ..., index: bool_t = ..., index_label: IndexLabel | None = ..., - mode: str = ..., + mode: Literal["w", "x", "a"] = ..., encoding: str | None = ..., compression: CompressionOptions = ..., quoting: int | None = ..., @@ -3586,7 +3587,7 @@ def to_csv( doublequote: bool_t = ..., escapechar: str | None = ..., decimal: str = ..., - errors: str = ..., + errors: OpenFileErrors = ..., storage_options: StorageOptions = ..., ) -> None: ... @@ -3606,7 +3607,7 @@ def to_csv( header: bool_t | list[str] = True, index: bool_t = True, index_label: IndexLabel | None = None, - mode: str = "w", + mode: Literal["w", "x", "a"] = "w", encoding: str | None = None, compression: CompressionOptions = "infer", quoting: int | None = None, @@ -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""" @@ -4852,8 +4853,8 @@ def sort_values( axis: Axis = ..., ascending: bool_t | Sequence[bool_t] = ..., inplace: Literal[False] = ..., - kind: str = ..., - na_position: str = ..., + kind: SortKind = ..., + na_position: NaPosition = ..., ignore_index: bool_t = ..., key: ValueKeyFunc = ..., ) -> Self: @@ -4866,8 +4867,8 @@ def sort_values( axis: Axis = ..., ascending: bool_t | Sequence[bool_t] = ..., inplace: Literal[True], - kind: str = ..., - na_position: str = ..., + kind: SortKind = ..., + na_position: NaPosition = ..., ignore_index: bool_t = ..., key: ValueKeyFunc = ..., ) -> None: @@ -4880,8 +4881,8 @@ def sort_values( axis: Axis = ..., ascending: bool_t | Sequence[bool_t] = ..., inplace: bool_t = ..., - kind: str = ..., - na_position: str = ..., + kind: SortKind = ..., + na_position: NaPosition = ..., ignore_index: bool_t = ..., key: ValueKeyFunc = ..., ) -> Self | None: @@ -4893,8 +4894,8 @@ def sort_values( axis: Axis = 0, ascending: bool_t | Sequence[bool_t] = True, inplace: bool_t = False, - kind: str = "quicksort", - na_position: str = "last", + kind: SortKind = "quicksort", + na_position: NaPosition = "last", ignore_index: bool_t = False, key: ValueKeyFunc = None, ) -> Self | None: From dc7fd11ec9cf3194c19466630209d50c0356d9ae Mon Sep 17 00:00:00 2001 From: Kevin Anker <34544392+kjanker@users.noreply.github.com> Date: Tue, 18 Apr 2023 15:31:30 +0000 Subject: [PATCH 2/5] lint code --- pandas/_typing.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index a9ed7ca76a49d..a99dc584f64ca 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -391,4 +391,12 @@ def closed(self) -> bool: AlignJoin = Literal["outer", "inner", "left", "right"] DtypeBackend = Literal["pyarrow", "numpy_nullable"] -OpenFileErrors = Literal["strict", "ignore", "replace", "surrogateescape", "xmlcharrefreplace", "backslashreplace", "namereplace"] +OpenFileErrors = Literal[ + "strict", + "ignore", + "replace", + "surrogateescape", + "xmlcharrefreplace", + "backslashreplace", + "namereplace", +] From 79538acc0314dd44e5d705198949cb58229cc690 Mon Sep 17 00:00:00 2001 From: Kevin Anker <34544392+kjanker@users.noreply.github.com> Date: Tue, 18 Apr 2023 15:50:54 +0000 Subject: [PATCH 3/5] lint code some more --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 0d1a81e08f61c..2f685aaf2c20a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2714,7 +2714,7 @@ def to_sql( index_label: IndexLabel = None, chunksize: int | None = None, dtype: DtypeArg | None = None, - method: Literal['multi'] | Callable | None = None, + method: Literal["multi"] | Callable | None = None, ) -> int | None: """ Write records stored in a DataFrame to a SQL database. From 30e72a9512f5c62343044e5ce87bbc7b2df3ad3e Mon Sep 17 00:00:00 2001 From: Kevin Anker <34544392+kjanker@users.noreply.github.com> Date: Tue, 18 Apr 2023 23:52:39 +0000 Subject: [PATCH 4/5] added sql method typing --- pandas/io/sql.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 0332cfb4adad7..d7f7a0b8801ed 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -23,6 +23,7 @@ from typing import ( TYPE_CHECKING, Any, + Callable, Iterator, Literal, Mapping, @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: From e7fb7c6a0c5b852326edf411c5bad41db780934a Mon Sep 17 00:00:00 2001 From: Kevin Anker <34544392+kjanker@users.noreply.github.com> Date: Wed, 19 Apr 2023 15:05:09 +0000 Subject: [PATCH 5/5] revert mode literals --- pandas/core/generic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2f685aaf2c20a..8d7d66a1f1504 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3549,7 +3549,7 @@ def to_csv( header: bool_t | list[str] = ..., index: bool_t = ..., index_label: IndexLabel | None = ..., - mode: Literal["w", "x", "a"] = ..., + mode: str = ..., encoding: str | None = ..., compression: CompressionOptions = ..., quoting: int | None = ..., @@ -3576,7 +3576,7 @@ def to_csv( header: bool_t | list[str] = ..., index: bool_t = ..., index_label: IndexLabel | None = ..., - mode: Literal["w", "x", "a"] = ..., + mode: str = ..., encoding: str | None = ..., compression: CompressionOptions = ..., quoting: int | None = ..., @@ -3607,7 +3607,7 @@ def to_csv( header: bool_t | list[str] = True, index: bool_t = True, index_label: IndexLabel | None = None, - mode: Literal["w", "x", "a"] = "w", + mode: str = "w", encoding: str | None = None, compression: CompressionOptions = "infer", quoting: int | None = None,