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..57e03dedc384d 100644 --- a/pandas/core/methods/to_dict.py +++ b/pandas/core/methods/to_dict.py @@ -155,7 +155,8 @@ def to_dict( stacklevel=find_stack_level(), ) # GH16122 - into_c = com.standardize_mapping(into) + # 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", # 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..c300db8c114c1 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], +) -> 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: 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/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..214d1d7079fdb 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,12 +25,14 @@ 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") -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 +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 + 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() @@ -136,7 +139,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() @@ -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,10 @@ def as_escaped_string( ) elif is_sequence(thing) and _nest_lvl < get_option("display.pprint_nest_depth"): result = _pprint_seq( - thing, + # 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, quote_strings=quote_strings, @@ -240,7 +246,7 @@ def as_escaped_string( def pprint_thing_encoded( - object, 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) @@ -252,7 +258,8 @@ def enable_data_resource_formatter(enable: bool) -> None: return from IPython import get_ipython - ip = get_ipython() + # 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 return @@ -289,7 +296,7 @@ def default_pprint(thing: Any, max_seq_items: int | None = None) -> str: def format_object_summary( - obj, + obj: ListLike, formatter: Callable, is_justify: bool = True, name: str | None = None, @@ -525,7 +532,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 +564,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