Skip to content

Fix some typing errors #57816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions pandas/_libs/ops.pyi
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
9 changes: 8 additions & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,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,
)

Expand Down Expand Up @@ -530,3 +534,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]
6 changes: 2 additions & 4 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import operator
from typing import (
TYPE_CHECKING,
Any,
Callable,
Literal,
TypeVar,
Expand Down Expand Up @@ -80,6 +79,7 @@

from pandas._typing import (
AnyArrayLike,
BinOp,
Dtype,
FillnaOptions,
NpDtype,
Expand Down Expand Up @@ -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.

Expand Down
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 @@ -14,10 +14,19 @@
import numpy as np

if TYPE_CHECKING:
from pandas._typing import npt
from pandas._typing import (
ArrayLike,
BoolOp,
Scalar,
npt,
)


def invalid_comparison(left, right, op) -> npt.NDArray[np.bool_]:
def invalid_comparison(
left: ArrayLike,
right: ArrayLike | Scalar,
op: BoolOp,
) -> 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
24 changes: 14 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,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,
Expand All @@ -240,7 +243,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 +255,8 @@ def enable_data_resource_formatter(enable: bool) -> None:
return
from IPython import get_ipython

ip = 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
return
Expand Down Expand Up @@ -289,7 +293,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 +529,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 +561,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
Loading
Loading