From a1f8df0e5e92d03764d4a69862a992bcb43adc02 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Mon, 11 Mar 2024 07:07:07 +0100 Subject: [PATCH 01/12] Fix some typing errors --- pandas/core/methods/selectn.py | 2 +- pandas/core/methods/to_dict.py | 2 +- pandas/core/ops/invalid.py | 15 ++++++++++++--- pandas/io/common.py | 6 +++--- pandas/io/excel/_odswriter.py | 12 +++++++----- pandas/io/formats/css.py | 12 +++++++++--- pandas/io/formats/printing.py | 16 ++++++++-------- pandas/plotting/_misc.py | 2 +- pyproject.toml | 6 ------ 9 files changed, 42 insertions(+), 31 deletions(-) diff --git a/pandas/core/methods/selectn.py b/pandas/core/methods/selectn.py index 20aec3ccadded..283acaca2c117 100644 --- a/pandas/core/methods/selectn.py +++ b/pandas/core/methods/selectn.py @@ -213,7 +213,7 @@ def compute(self, method: str) -> DataFrame: f"cannot use method {method!r} with this dtype" ) - def get_indexer(current_indexer, other_indexer): + def get_indexer(current_indexer: Index, other_indexer: Index) -> Index: """ Helper function to concat `current_indexer` and `other_indexer` depending on `method` diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py index a5833514a9799..4a30c6fa35f2a 100644 --- a/pandas/core/methods/to_dict.py +++ b/pandas/core/methods/to_dict.py @@ -155,7 +155,7 @@ def to_dict( stacklevel=find_stack_level(), ) # GH16122 - into_c = com.standardize_mapping(into) + into_c = com.standardize_mapping(into) # type: ignore[no-untyped-call] # error: Incompatible types in assignment (expression has type "str", # variable has type "Literal['dict', 'list', 'series', 'split', 'tight', diff --git a/pandas/core/ops/invalid.py b/pandas/core/ops/invalid.py index 7b3af99ee1a95..2f8990293fd30 100644 --- a/pandas/core/ops/invalid.py +++ b/pandas/core/ops/invalid.py @@ -7,6 +7,7 @@ import operator from typing import ( TYPE_CHECKING, + Any, Callable, NoReturn, ) @@ -14,10 +15,18 @@ import numpy as np if TYPE_CHECKING: - from pandas._typing import npt + from pandas._typing import ( + ArrayLike, + Scalar, + npt, + ) -def invalid_comparison(left, right, op) -> npt.NDArray[np.bool_]: +def invalid_comparison( + left: ArrayLike, + right: ArrayLike | Scalar, + op: Callable[[Any, Any], bool], # Can we reuse _BoolOp here? +) -> npt.NDArray[np.bool_]: """ If a comparison has mismatched types and is not necessarily meaningful, follow python3 conventions by: @@ -59,7 +68,7 @@ def make_invalid_op(name: str) -> Callable[..., NoReturn]: invalid_op : function """ - def invalid_op(self, other=None) -> NoReturn: + def invalid_op(self: Any, other: Any = None) -> NoReturn: typ = type(self).__name__ raise TypeError(f"cannot perform {name} with this index type: {typ}") diff --git a/pandas/io/common.py b/pandas/io/common.py index 3544883afedd6..abeb789a4b778 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -278,7 +278,7 @@ def stringify_path( return _expand_user(filepath_or_buffer) -def urlopen(*args, **kwargs): +def urlopen(*args: Any, **kwargs: Any) -> Any: """ Lazy-import wrapper for stdlib urlopen, as that imports a big chunk of the stdlib. @@ -972,7 +972,7 @@ def __init__( mode: Literal["r", "a", "w", "x"] = "r", fileobj: ReadBuffer[bytes] | WriteBuffer[bytes] | None = None, archive_name: str | None = None, - **kwargs, + **kwargs: Any, ) -> None: super().__init__() self.archive_name = archive_name @@ -1025,7 +1025,7 @@ def __init__( file: FilePath | ReadBuffer[bytes] | WriteBuffer[bytes], mode: str, archive_name: str | None = None, - **kwargs, + **kwargs: Any, ) -> None: super().__init__() mode = mode.replace("b", "") diff --git a/pandas/io/excel/_odswriter.py b/pandas/io/excel/_odswriter.py index cdb22a57399ed..0ddb59d3413ff 100644 --- a/pandas/io/excel/_odswriter.py +++ b/pandas/io/excel/_odswriter.py @@ -18,6 +18,8 @@ ) if TYPE_CHECKING: + from odf.opendocument import OpenDocumentSpreadsheet + from pandas._typing import ( ExcelWriterIfSheetExists, FilePath, @@ -37,12 +39,12 @@ def __init__( path: FilePath | WriteExcelBuffer | ExcelWriter, engine: str | None = None, date_format: str | None = None, - datetime_format=None, + datetime_format: str | None = None, mode: str = "w", storage_options: StorageOptions | None = None, if_sheet_exists: ExcelWriterIfSheetExists | None = None, engine_kwargs: dict[str, Any] | None = None, - **kwargs, + **kwargs: Any, ) -> None: from odf.opendocument import OpenDocumentSpreadsheet @@ -63,7 +65,7 @@ def __init__( self._style_dict: dict[str, str] = {} @property - def book(self): + def book(self) -> OpenDocumentSpreadsheet: """ Book instance of class odf.opendocument.OpenDocumentSpreadsheet. @@ -149,7 +151,7 @@ def _write_cells( for row_nr in range(max(rows.keys()) + 1): wks.addElement(rows[row_nr]) - def _make_table_cell_attributes(self, cell) -> dict[str, int | str]: + def _make_table_cell_attributes(self, cell: ExcelCell) -> dict[str, int | str]: """Convert cell attributes to OpenDocument attributes Parameters @@ -171,7 +173,7 @@ def _make_table_cell_attributes(self, cell) -> dict[str, int | str]: attributes["numbercolumnsspanned"] = cell.mergeend return attributes - def _make_table_cell(self, cell) -> tuple[object, Any]: + def _make_table_cell(self, cell: ExcelCell) -> tuple[object, Any]: """Convert cell data to an OpenDocument spreadsheet cell Parameters diff --git a/pandas/io/formats/css.py b/pandas/io/formats/css.py index d3f4072b2ff08..d3d0da6f562a7 100644 --- a/pandas/io/formats/css.py +++ b/pandas/io/formats/css.py @@ -36,7 +36,9 @@ def _side_expander(prop_fmt: str) -> Callable: function: Return to call when a 'border(-{side}): {value}' string is encountered """ - def expand(self, prop: str, value: str) -> Generator[tuple[str, str], None, None]: + def expand( + self: CSSResolver, prop: str, value: str + ) -> Generator[tuple[str, str], None, None]: """ Expand shorthand property into side-specific property (top, right, bottom, left) @@ -81,7 +83,9 @@ def _border_expander(side: str = "") -> Callable: if side != "": side = f"-{side}" - def expand(self, prop: str, value: str) -> Generator[tuple[str, str], None, None]: + def expand( + self: CSSResolver, prop: str, value: str + ) -> Generator[tuple[str, str], None, None]: """ Expand border into color, style, and width tuples @@ -343,7 +347,9 @@ def _update_other_units(self, props: dict[str, str]) -> dict[str, str]: ) return props - def size_to_pt(self, in_val, em_pt=None, conversions=UNIT_RATIOS) -> str: + def size_to_pt( + self, in_val: str, em_pt: float | None = None, conversions: dict = UNIT_RATIOS + ) -> str: def _error() -> str: warnings.warn( f"Unhandled size: {in_val!r}", diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index b30351e14332d..4e7e1ea80ae82 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -29,7 +29,7 @@ _VT = TypeVar("_VT") -def adjoin(space: int, *lists: list[str], **kwargs) -> str: +def adjoin(space: int, *lists: list[str], **kwargs: Any) -> str: """ Glues together two sets of strings using the amount of space requested. The idea is to prettify. @@ -98,7 +98,7 @@ def _adj_justify(texts: Iterable[str], max_len: int, mode: str = "right") -> lis def _pprint_seq( - seq: Sequence, _nest_lvl: int = 0, max_seq_items: int | None = None, **kwds + seq: Sequence, _nest_lvl: int = 0, max_seq_items: int | None = None, **kwds: Any ) -> str: """ internal. pprinter for iterables. you should probably use pprint_thing() @@ -136,7 +136,7 @@ def _pprint_seq( def _pprint_dict( - seq: Mapping, _nest_lvl: int = 0, max_seq_items: int | None = None, **kwds + seq: Mapping, _nest_lvl: int = 0, max_seq_items: int | None = None, **kwds: Any ) -> str: """ internal. pprinter for iterables. you should probably use pprint_thing() @@ -240,7 +240,7 @@ def as_escaped_string( def pprint_thing_encoded( - object, encoding: str = "utf-8", errors: str = "replace" + object: Any, encoding: str = "utf-8", errors: str = "replace" ) -> bytes: value = pprint_thing(object) # get unicode representation of object return value.encode(encoding, errors) @@ -252,7 +252,7 @@ def enable_data_resource_formatter(enable: bool) -> None: return from IPython import get_ipython - ip = get_ipython() + ip = get_ipython() # type: ignore[no-untyped-call] if ip is None: # still not in IPython return @@ -289,7 +289,7 @@ def default_pprint(thing: Any, max_seq_items: int | None = None) -> str: def format_object_summary( - obj, + obj: Any, formatter: Callable, is_justify: bool = True, name: str | None = None, @@ -525,7 +525,7 @@ def justify(self, texts: Any, max_len: int, mode: str = "right") -> list[str]: else: return [x.rjust(max_len) for x in texts] - def adjoin(self, space: int, *lists, **kwargs) -> str: + def adjoin(self, space: int, *lists: Any, **kwargs: Any) -> str: return adjoin(space, *lists, strlen=self.len, justfunc=self.justify, **kwargs) @@ -557,7 +557,7 @@ def justify( self, texts: Iterable[str], max_len: int, mode: str = "right" ) -> list[str]: # re-calculate padding space per str considering East Asian Width - def _get_pad(t): + def _get_pad(t: str) -> int: return max_len - self.len(t) + len(t) if mode == "left": diff --git a/pandas/plotting/_misc.py b/pandas/plotting/_misc.py index 16192fda07bad..38fa0ff75cf66 100644 --- a/pandas/plotting/_misc.py +++ b/pandas/plotting/_misc.py @@ -672,7 +672,7 @@ def reset(self) -> None: # error: Cannot access "__init__" directly self.__init__() # type: ignore[misc] - def _get_canonical_key(self, key): + def _get_canonical_key(self, key: str) -> str: return self._ALIASES.get(key, key) @contextmanager diff --git a/pyproject.toml b/pyproject.toml index bbcaa73b55ff8..f96fbee4a5818 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -594,10 +594,8 @@ module = [ "pandas.core.interchange.dataframe_protocol", # TODO "pandas.core.interchange.from_dataframe", # TODO "pandas.core.internals.*", # TODO - "pandas.core.methods.*", # TODO "pandas.core.ops.array_ops", # TODO "pandas.core.ops.common", # TODO - "pandas.core.ops.invalid", # TODO "pandas.core.ops.missing", # TODO "pandas.core.reshape.*", # TODO "pandas.core.strings.*", # TODO @@ -630,15 +628,12 @@ module = [ "pandas.io.clipboard", # TODO "pandas.io.excel._base", # TODO "pandas.io.excel._odfreader", # TODO - "pandas.io.excel._odswriter", # TODO "pandas.io.excel._openpyxl", # TODO "pandas.io.excel._pyxlsb", # TODO "pandas.io.excel._xlrd", # TODO "pandas.io.excel._xlsxwriter", # TODO - "pandas.io.formats.css", # TODO "pandas.io.formats.excel", # TODO "pandas.io.formats.format", # TODO - "pandas.io.formats.printing", # TODO "pandas.io.formats.style", # TODO "pandas.io.formats.style_render", # TODO "pandas.io.formats.xml", # TODO @@ -647,7 +642,6 @@ module = [ "pandas.io.sas.sas_xport", # TODO "pandas.io.sas.sas7bdat", # TODO "pandas.io.clipboards", # TODO - "pandas.io.common", # TODO "pandas.io.html", # TODO "pandas.io.parquet", # TODO "pandas.io.pytables", # TODO From 215cc533e80508eee0620ea5399255cf1050d59a Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 06:43:09 +0100 Subject: [PATCH 02/12] Review --- pandas/core/ops/invalid.py | 2 +- pandas/io/formats/printing.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pandas/core/ops/invalid.py b/pandas/core/ops/invalid.py index 2f8990293fd30..31424232fbea8 100644 --- a/pandas/core/ops/invalid.py +++ b/pandas/core/ops/invalid.py @@ -68,7 +68,7 @@ def make_invalid_op(name: str) -> Callable[..., NoReturn]: invalid_op : function """ - def invalid_op(self: Any, other: Any = None) -> NoReturn: + def invalid_op(self: object, other: object = None) -> NoReturn: typ = type(self).__name__ raise TypeError(f"cannot perform {name} with this index type: {typ}") diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index 4e7e1ea80ae82..4dcff41299e59 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -11,6 +11,7 @@ ) import sys from typing import ( + TYPE_CHECKING, Any, Callable, TypeVar, @@ -24,6 +25,8 @@ from pandas.io.formats.console import get_console_size +if TYPE_CHECKING: + from pandas._typing import ListLike EscapeChars = Union[Mapping[str, str], Iterable[str]] _KT = TypeVar("_KT") _VT = TypeVar("_VT") @@ -98,7 +101,7 @@ def _adj_justify(texts: Iterable[str], max_len: int, mode: str = "right") -> lis def _pprint_seq( - seq: Sequence, _nest_lvl: int = 0, max_seq_items: int | None = None, **kwds: Any + seq: ListLike, _nest_lvl: int = 0, max_seq_items: int | None = None, **kwds: Any ) -> str: """ internal. pprinter for iterables. you should probably use pprint_thing() @@ -167,7 +170,7 @@ def _pprint_dict( def pprint_thing( - thing: Any, + thing: object, _nest_lvl: int = 0, escape_chars: EscapeChars | None = None, default_escapes: bool = False, @@ -225,7 +228,7 @@ def as_escaped_string( ) elif is_sequence(thing) and _nest_lvl < get_option("display.pprint_nest_depth"): result = _pprint_seq( - thing, + thing, # type: ignore[arg-type] _nest_lvl, escape_chars=escape_chars, quote_strings=quote_strings, @@ -240,7 +243,7 @@ def as_escaped_string( def pprint_thing_encoded( - object: Any, encoding: str = "utf-8", errors: str = "replace" + object: object, encoding: str = "utf-8", errors: str = "replace" ) -> bytes: value = pprint_thing(object) # get unicode representation of object return value.encode(encoding, errors) @@ -289,7 +292,7 @@ def default_pprint(thing: Any, max_seq_items: int | None = None) -> str: def format_object_summary( - obj: Any, + obj: ListLike, formatter: Callable, is_justify: bool = True, name: str | None = None, From 89dec5051ea5f276159d0d26be75fe014d0458a0 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 14:33:59 +0100 Subject: [PATCH 03/12] Reuse types --- pandas/_libs/ops.pyi | 20 +++++++++----------- pandas/_typing.py | 4 ++++ pandas/core/arrays/period.py | 6 ++---- pandas/core/ops/invalid.py | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pandas/_libs/ops.pyi b/pandas/_libs/ops.pyi index 6738a1dff4a9e..e62ed81c4ea54 100644 --- a/pandas/_libs/ops.pyi +++ b/pandas/_libs/ops.pyi @@ -1,38 +1,36 @@ from typing import ( - Any, - Callable, Iterable, Literal, - TypeAlias, overload, ) import numpy as np -from pandas._typing import npt - -_BinOp: TypeAlias = Callable[[Any, Any], Any] -_BoolOp: TypeAlias = Callable[[Any, Any], bool] +from pandas._typing import ( + BinOp, + BoolOp, + npt, +) def scalar_compare( values: np.ndarray, # object[:] val: object, - op: _BoolOp, # {operator.eq, operator.ne, ...} + op: BoolOp, # {operator.eq, operator.ne, ...} ) -> npt.NDArray[np.bool_]: ... def vec_compare( left: npt.NDArray[np.object_], right: npt.NDArray[np.object_], - op: _BoolOp, # {operator.eq, operator.ne, ...} + op: BoolOp, # {operator.eq, operator.ne, ...} ) -> npt.NDArray[np.bool_]: ... def scalar_binop( values: np.ndarray, # object[:] val: object, - op: _BinOp, # binary operator + op: BinOp, # binary operator ) -> np.ndarray: ... def vec_binop( left: np.ndarray, # object[:] right: np.ndarray, # object[:] - op: _BinOp, # binary operator + op: BinOp, # binary operator ) -> np.ndarray: ... @overload def maybe_convert_bool( diff --git a/pandas/_typing.py b/pandas/_typing.py index f868a92554b39..b48261b558d38 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -23,6 +23,7 @@ Optional, Protocol, Type as type_t, + TypeAlias, TypeVar, Union, overload, @@ -530,3 +531,6 @@ def closed(self) -> bool: # maintaine the sub-type of any hashable sequence SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable]) + +BinOp: TypeAlias = Callable[[Any, Any], Any] +BoolOp: TypeAlias = Callable[[Any, Any], bool] diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index d05f857f46179..771c97d0f65af 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -4,7 +4,6 @@ import operator from typing import ( TYPE_CHECKING, - Any, Callable, Literal, TypeVar, @@ -80,6 +79,7 @@ from pandas._typing import ( AnyArrayLike, + BinOp, Dtype, FillnaOptions, NpDtype, @@ -863,9 +863,7 @@ def fillna( # ------------------------------------------------------------------ # Arithmetic Methods - def _addsub_int_array_or_scalar( - self, other: np.ndarray | int, op: Callable[[Any, Any], Any] - ) -> Self: + def _addsub_int_array_or_scalar(self, other: np.ndarray | int, op: BinOp) -> Self: """ Add or subtract array of integers. diff --git a/pandas/core/ops/invalid.py b/pandas/core/ops/invalid.py index 31424232fbea8..23ad68d31cf94 100644 --- a/pandas/core/ops/invalid.py +++ b/pandas/core/ops/invalid.py @@ -7,7 +7,6 @@ import operator from typing import ( TYPE_CHECKING, - Any, Callable, NoReturn, ) @@ -17,6 +16,7 @@ if TYPE_CHECKING: from pandas._typing import ( ArrayLike, + BoolOp, Scalar, npt, ) @@ -25,7 +25,7 @@ def invalid_comparison( left: ArrayLike, right: ArrayLike | Scalar, - op: Callable[[Any, Any], bool], # Can we reuse _BoolOp here? + op: BoolOp, ) -> npt.NDArray[np.bool_]: """ If a comparison has mismatched types and is not necessarily meaningful, From 0fcb8cd51b923fafb341acb90c7d19c4be3c905b Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 14:45:29 +0100 Subject: [PATCH 04/12] Reuse types --- pandas/_libs/ops.pyi | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/ops.pyi b/pandas/_libs/ops.pyi index e62ed81c4ea54..1fb920d461f17 100644 --- a/pandas/_libs/ops.pyi +++ b/pandas/_libs/ops.pyi @@ -1,4 +1,5 @@ from typing import ( + TYPE_CHECKING, Iterable, Literal, overload, @@ -6,11 +7,12 @@ from typing import ( import numpy as np -from pandas._typing import ( - BinOp, - BoolOp, - npt, -) +if TYPE_CHECKING: # noqa: PYI002 + from pandas._typing import ( + BinOp, + BoolOp, + npt, + ) def scalar_compare( values: np.ndarray, # object[:] From 0e9e7bc72dc5bfdc14e27e61983aecd7aee2e102 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 14:52:08 +0100 Subject: [PATCH 05/12] Reuse types --- pandas/_libs/ops.pyi | 12 +++++------- pandas/_typing.py | 7 +++++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pandas/_libs/ops.pyi b/pandas/_libs/ops.pyi index 1fb920d461f17..e62ed81c4ea54 100644 --- a/pandas/_libs/ops.pyi +++ b/pandas/_libs/ops.pyi @@ -1,5 +1,4 @@ from typing import ( - TYPE_CHECKING, Iterable, Literal, overload, @@ -7,12 +6,11 @@ from typing import ( import numpy as np -if TYPE_CHECKING: # noqa: PYI002 - from pandas._typing import ( - BinOp, - BoolOp, - npt, - ) +from pandas._typing import ( + BinOp, + BoolOp, + npt, +) def scalar_compare( values: np.ndarray, # object[:] diff --git a/pandas/_typing.py b/pandas/_typing.py index b48261b558d38..e78befc427ec5 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -23,7 +23,6 @@ Optional, Protocol, Type as type_t, - TypeAlias, TypeVar, Union, overload, @@ -94,13 +93,17 @@ from typing import SupportsIndex if sys.version_info >= (3, 10): + from typing import ( + ParamSpec, + TypeAlias, + ) from typing import Concatenate # pyright: ignore[reportUnusedImport] - from typing import ParamSpec from typing import TypeGuard # pyright: ignore[reportUnusedImport] else: from typing_extensions import ( # pyright: ignore[reportUnusedImport] Concatenate, ParamSpec, + TypeAlias, TypeGuard, ) From d4dbffad3ec16bba61da09d05e0ac3a46ef1abb9 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 21:22:19 +0100 Subject: [PATCH 06/12] Add error message --- pandas/core/methods/to_dict.py | 1 + pandas/io/formats/printing.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py index 4a30c6fa35f2a..57e03dedc384d 100644 --- a/pandas/core/methods/to_dict.py +++ b/pandas/core/methods/to_dict.py @@ -155,6 +155,7 @@ def to_dict( stacklevel=find_stack_level(), ) # GH16122 + # error: Call to untyped function "standardize_mapping" in typed context into_c = com.standardize_mapping(into) # type: ignore[no-untyped-call] # error: Incompatible types in assignment (expression has type "str", diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index 4dcff41299e59..ee937909f7e2d 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -255,6 +255,7 @@ def enable_data_resource_formatter(enable: bool) -> None: return from IPython import get_ipython + # error: Call to untyped function "standardize_mapping" in typed context ip = get_ipython() # type: ignore[no-untyped-call] if ip is None: # still not in IPython From d26d109b947263cf9016d8e6e149dc7c017d8a96 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 21:35:00 +0100 Subject: [PATCH 07/12] Add error message --- pandas/io/formats/printing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index ee937909f7e2d..629a2dfbbd8b7 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -255,7 +255,7 @@ def enable_data_resource_formatter(enable: bool) -> None: return from IPython import get_ipython - # error: Call to untyped function "standardize_mapping" in typed context + # error: Call to untyped function "get_ipython" in typed context ip = get_ipython() # type: ignore[no-untyped-call] if ip is None: # still not in IPython From bf56206f23c7681c8b3122c84fc9a6bd71cbafd5 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 23:13:44 +0100 Subject: [PATCH 08/12] Revert "Reuse types" This reverts commit 0e9e7bc72dc5bfdc14e27e61983aecd7aee2e102. --- pandas/_libs/ops.pyi | 12 +++++++----- pandas/_typing.py | 7 ++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/pandas/_libs/ops.pyi b/pandas/_libs/ops.pyi index e62ed81c4ea54..1fb920d461f17 100644 --- a/pandas/_libs/ops.pyi +++ b/pandas/_libs/ops.pyi @@ -1,4 +1,5 @@ from typing import ( + TYPE_CHECKING, Iterable, Literal, overload, @@ -6,11 +7,12 @@ from typing import ( import numpy as np -from pandas._typing import ( - BinOp, - BoolOp, - npt, -) +if TYPE_CHECKING: # noqa: PYI002 + from pandas._typing import ( + BinOp, + BoolOp, + npt, + ) def scalar_compare( values: np.ndarray, # object[:] diff --git a/pandas/_typing.py b/pandas/_typing.py index e78befc427ec5..b48261b558d38 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -23,6 +23,7 @@ Optional, Protocol, Type as type_t, + TypeAlias, TypeVar, Union, overload, @@ -93,17 +94,13 @@ from typing import SupportsIndex if sys.version_info >= (3, 10): - from typing import ( - ParamSpec, - TypeAlias, - ) from typing import Concatenate # pyright: ignore[reportUnusedImport] + from typing import ParamSpec from typing import TypeGuard # pyright: ignore[reportUnusedImport] else: from typing_extensions import ( # pyright: ignore[reportUnusedImport] Concatenate, ParamSpec, - TypeAlias, TypeGuard, ) From f09a2a707cd196db4766571a1669847509c86432 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 23:14:20 +0100 Subject: [PATCH 09/12] Revert "Reuse types" This reverts commit 0fcb8cd51b923fafb341acb90c7d19c4be3c905b. --- pandas/_libs/ops.pyi | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/ops.pyi b/pandas/_libs/ops.pyi index 1fb920d461f17..e62ed81c4ea54 100644 --- a/pandas/_libs/ops.pyi +++ b/pandas/_libs/ops.pyi @@ -1,5 +1,4 @@ from typing import ( - TYPE_CHECKING, Iterable, Literal, overload, @@ -7,12 +6,11 @@ from typing import ( import numpy as np -if TYPE_CHECKING: # noqa: PYI002 - from pandas._typing import ( - BinOp, - BoolOp, - npt, - ) +from pandas._typing import ( + BinOp, + BoolOp, + npt, +) def scalar_compare( values: np.ndarray, # object[:] From 173e27786d59bb7296f22c5d984122ff8e9b7d73 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 23:14:36 +0100 Subject: [PATCH 10/12] Revert "Reuse types" This reverts commit 89dec5051ea5f276159d0d26be75fe014d0458a0. --- pandas/_libs/ops.pyi | 20 +++++++++++--------- pandas/_typing.py | 4 ---- pandas/core/arrays/period.py | 6 ++++-- pandas/core/ops/invalid.py | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pandas/_libs/ops.pyi b/pandas/_libs/ops.pyi index e62ed81c4ea54..6738a1dff4a9e 100644 --- a/pandas/_libs/ops.pyi +++ b/pandas/_libs/ops.pyi @@ -1,36 +1,38 @@ from typing import ( + Any, + Callable, Iterable, Literal, + TypeAlias, overload, ) import numpy as np -from pandas._typing import ( - BinOp, - BoolOp, - npt, -) +from pandas._typing import npt + +_BinOp: TypeAlias = Callable[[Any, Any], Any] +_BoolOp: TypeAlias = Callable[[Any, Any], bool] def scalar_compare( values: np.ndarray, # object[:] val: object, - op: BoolOp, # {operator.eq, operator.ne, ...} + op: _BoolOp, # {operator.eq, operator.ne, ...} ) -> npt.NDArray[np.bool_]: ... def vec_compare( left: npt.NDArray[np.object_], right: npt.NDArray[np.object_], - op: BoolOp, # {operator.eq, operator.ne, ...} + op: _BoolOp, # {operator.eq, operator.ne, ...} ) -> npt.NDArray[np.bool_]: ... def scalar_binop( values: np.ndarray, # object[:] val: object, - op: BinOp, # binary operator + op: _BinOp, # binary operator ) -> np.ndarray: ... def vec_binop( left: np.ndarray, # object[:] right: np.ndarray, # object[:] - op: BinOp, # binary operator + op: _BinOp, # binary operator ) -> np.ndarray: ... @overload def maybe_convert_bool( diff --git a/pandas/_typing.py b/pandas/_typing.py index b48261b558d38..f868a92554b39 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -23,7 +23,6 @@ Optional, Protocol, Type as type_t, - TypeAlias, TypeVar, Union, overload, @@ -531,6 +530,3 @@ def closed(self) -> bool: # maintaine the sub-type of any hashable sequence SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable]) - -BinOp: TypeAlias = Callable[[Any, Any], Any] -BoolOp: TypeAlias = Callable[[Any, Any], bool] diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 771c97d0f65af..d05f857f46179 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -4,6 +4,7 @@ import operator from typing import ( TYPE_CHECKING, + Any, Callable, Literal, TypeVar, @@ -79,7 +80,6 @@ from pandas._typing import ( AnyArrayLike, - BinOp, Dtype, FillnaOptions, NpDtype, @@ -863,7 +863,9 @@ def fillna( # ------------------------------------------------------------------ # Arithmetic Methods - def _addsub_int_array_or_scalar(self, other: np.ndarray | int, op: BinOp) -> Self: + def _addsub_int_array_or_scalar( + self, other: np.ndarray | int, op: Callable[[Any, Any], Any] + ) -> Self: """ Add or subtract array of integers. diff --git a/pandas/core/ops/invalid.py b/pandas/core/ops/invalid.py index 23ad68d31cf94..31424232fbea8 100644 --- a/pandas/core/ops/invalid.py +++ b/pandas/core/ops/invalid.py @@ -7,6 +7,7 @@ import operator from typing import ( TYPE_CHECKING, + Any, Callable, NoReturn, ) @@ -16,7 +17,6 @@ if TYPE_CHECKING: from pandas._typing import ( ArrayLike, - BoolOp, Scalar, npt, ) @@ -25,7 +25,7 @@ def invalid_comparison( left: ArrayLike, right: ArrayLike | Scalar, - op: BoolOp, + op: Callable[[Any, Any], bool], # Can we reuse _BoolOp here? ) -> npt.NDArray[np.bool_]: """ If a comparison has mismatched types and is not necessarily meaningful, From 929bc31bd2039bf8dda6e021a83d673ea21c2b6c Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 23:15:33 +0100 Subject: [PATCH 11/12] Remove comment --- pandas/core/ops/invalid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/ops/invalid.py b/pandas/core/ops/invalid.py index 31424232fbea8..c300db8c114c1 100644 --- a/pandas/core/ops/invalid.py +++ b/pandas/core/ops/invalid.py @@ -25,7 +25,7 @@ def invalid_comparison( left: ArrayLike, right: ArrayLike | Scalar, - op: Callable[[Any, Any], bool], # Can we reuse _BoolOp here? + op: Callable[[Any, Any], bool], ) -> npt.NDArray[np.bool_]: """ If a comparison has mismatched types and is not necessarily meaningful, From 20b181d64913fd9bb7b3bea1d58e45ade0390e68 Mon Sep 17 00:00:00 2001 From: Anh Trinh Date: Tue, 12 Mar 2024 23:20:53 +0100 Subject: [PATCH 12/12] Add error message --- pandas/io/formats/printing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index 629a2dfbbd8b7..214d1d7079fdb 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -228,6 +228,9 @@ def as_escaped_string( ) elif is_sequence(thing) and _nest_lvl < get_option("display.pprint_nest_depth"): result = _pprint_seq( + # error: Argument 1 to "_pprint_seq" has incompatible type "object"; + # expected "ExtensionArray | ndarray[Any, Any] | Index | Series | + # SequenceNotStr[Any] | range" thing, # type: ignore[arg-type] _nest_lvl, escape_chars=escape_chars,