Skip to content

TYP: Simple type fixes for ExtensionArray #41255

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
May 6, 2021
19 changes: 13 additions & 6 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
TYPE_CHECKING,
Any,
Callable,
Iterator,
Sequence,
TypeVar,
cast,
Expand Down Expand Up @@ -69,6 +70,7 @@
)

if TYPE_CHECKING:
from typing import Literal

class ExtensionArraySupportsAnyAll("ExtensionArray"):
def any(self, *, skipna: bool = True) -> bool:
Expand Down Expand Up @@ -375,7 +377,7 @@ def __len__(self) -> int:
"""
raise AbstractMethodError(self)

def __iter__(self):
def __iter__(self) -> Iterator[Any]:
"""
Iterate over elements of the array.
"""
Expand All @@ -385,7 +387,7 @@ def __iter__(self):
for i in range(len(self)):
yield self[i]

def __contains__(self, item) -> bool | np.bool_:
def __contains__(self, item: Any) -> bool | np.bool_:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we type this with Label elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback I did a grep and only saw Any

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Label is for index values, this is an array, so Any is correct I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Label was an alias for Union[Hashable, None] to workaround a mypy bug that has since been fixed. Now mypy recognizes None as being Hashable.

wrt Any

from https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#incomplete-stubs

Included functions and methods must list all arguments, but the arguments can be left unannotated. Do not use Any to mark unannotated arguments or return values.

from https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions

When adding type hints, avoid using the Any type when possible. Reserve the use of Any for when:

the correct type cannot be expressed in the current type system; and
to avoid Union returns (see above).

Note that Any is not the correct type to use if you want to indicate that some function can accept literally anything: in those cases use object instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on that explanation, it should be object here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used object, but had to do a type ignore when any() was called on a comparison. I couldn't figure out how to work around that. See latest commit.

"""
Return for `item in self`.
"""
Expand Down Expand Up @@ -680,7 +682,12 @@ def argmax(self, skipna: bool = True) -> int:
raise NotImplementedError
return nargminmax(self, "argmax")

def fillna(self, value=None, method=None, limit=None):
def fillna(
self,
value: Any | ArrayLike | None = None,
method: Literal["backfill", "bfill", "ffill", "pad"] | None = None,
limit: int | None = None,
):
"""
Fill NA/NaN values using the specified method.

Expand Down Expand Up @@ -1207,7 +1214,7 @@ def _formatter(self, boxed: bool = False) -> Callable[[Any], str | None]:
# Reshaping
# ------------------------------------------------------------------------

def transpose(self, *axes) -> ExtensionArray:
def transpose(self, *axes: tuple[int] | list[int] | None) -> ExtensionArray:
"""
Return a transposed view on this array.

Expand All @@ -1220,7 +1227,7 @@ def transpose(self, *axes) -> ExtensionArray:
def T(self) -> ExtensionArray:
return self.transpose()

def ravel(self, order="C") -> ExtensionArray:
def ravel(self, order: Literal["C", "F", "A", "K"] | None = "C") -> ExtensionArray:
"""
Return a flattened view on this array.

Expand Down Expand Up @@ -1294,7 +1301,7 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
"""
raise TypeError(f"cannot perform {name} with type {self.dtype}")

def __hash__(self):
def __hash__(self) -> int:
raise TypeError(f"unhashable type: {repr(type(self).__name__)}")

# ------------------------------------------------------------------------
Expand Down