Skip to content

Commit

Permalink
Fix some typing errors (pandas-dev#57816)
Browse files Browse the repository at this point in the history
* Fix some typing errors

* Review

* Reuse types

* Reuse types

* Reuse types

* Add error message

* Add error message

* Revert "Reuse types"

This reverts commit 0e9e7bc.

* Revert "Reuse types"

This reverts commit 0fcb8cd.

* Revert "Reuse types"

This reverts commit 89dec50.

* Remove comment

* Add error message
  • Loading branch information
tqa236 authored and pmhatre1 committed May 7, 2024
1 parent c55e294 commit 984cac3
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pandas/core/methods/selectn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/methods/to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 12 additions & 3 deletions pandas/core/ops/invalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@
import operator
from typing import (
TYPE_CHECKING,
Any,
Callable,
NoReturn,
)

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:
Expand Down Expand Up @@ -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}")

Expand Down
6 changes: 3 additions & 3 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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", "")
Expand Down
12 changes: 7 additions & 5 deletions pandas/io/excel/_odswriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
)

if TYPE_CHECKING:
from odf.opendocument import OpenDocumentSpreadsheet

from pandas._typing import (
ExcelWriterIfSheetExists,
FilePath,
Expand All @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions pandas/io/formats/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}",
Expand Down
27 changes: 17 additions & 10 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
import sys
from typing import (
TYPE_CHECKING,
Any,
Callable,
TypeVar,
Expand All @@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 984cac3

Please sign in to comment.