Skip to content

TYP: Update type hints for ExtensionArray and ExtensionDtype #39501

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

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f2c52a4
small typing fixes
Dr-Irv Jan 23, 2021
d7ff8d3
fix ExtensionArray and EXtensionDtype
Dr-Irv Jan 23, 2021
49fa06e
merge with master
Dr-Irv Jan 31, 2021
03b2c4a
fixes for delete, isin, unique
Dr-Irv Jan 31, 2021
3e19958
fix import of Literal
Dr-Irv Jan 31, 2021
6861901
remove quotes on ExtensionDType.construct_from_string
Dr-Irv Jan 31, 2021
9be6486
move numpy workaround to _typing.py
Dr-Irv Feb 1, 2021
260b367
remove numpy dummy
Dr-Irv Feb 2, 2021
6276725
remove extra line in _typing
Dr-Irv Feb 2, 2021
4dafaca
Merge remote-tracking branch 'upstream/master' into extensiontyping
Dr-Irv Feb 3, 2021
8b2cee2
import Literal
Dr-Irv Feb 3, 2021
3a7d839
Merge remote-tracking branch 'upstream/master' into extensiontyping
Dr-Irv Feb 14, 2021
a21bb60
merge with master
Dr-Irv Mar 8, 2021
8cd6b76
isort precommit fix
Dr-Irv Mar 8, 2021
e0e0131
fix interval.repeat() typing
Dr-Irv Mar 8, 2021
6a6a21f
overload for __getitem__ and use pattern with ExtensionArrayT as self…
Dr-Irv Mar 9, 2021
bf753e6
lose less ExtensionArrayT. Make registry private. consolidate overload
Dr-Irv Mar 10, 2021
c9795a5
remove ExtensionArray typing of self
Dr-Irv Mar 10, 2021
d452842
Merge remote-tracking branch 'upstream/master' into extensiontyping
Dr-Irv Mar 10, 2021
3c2c78b
merge with upstream/master
Dr-Irv Mar 12, 2021
548c198
make extension arrays work with new typing, fixing astype and to_numpy
Dr-Irv Mar 12, 2021
db8ed9b
fix Literal import
Dr-Irv Mar 12, 2021
f8191f8
fix logic in ensure_int_or_float
Dr-Irv Mar 12, 2021
575645f
fix conflict with master
Dr-Irv Mar 12, 2021
6f8fcb5
fix typing in groupby to_numpy call
Dr-Irv Mar 12, 2021
3ea2420
fix groupby again. Allow kwargs for extension to_numpy
Dr-Irv Mar 13, 2021
c83a628
Merge remote-tracking branch 'upstream/master' into extensiontyping
simonjayhawkins Mar 13, 2021
5bb24d4
fixes for merge with master
Dr-Irv Mar 13, 2021
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
10 changes: 7 additions & 3 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,13 @@ def _get_ilevel_values(index, level):
# skip exact index checking when `check_categorical` is False
if check_exact and check_categorical:
if not left.equals(right):
diff = (
np.sum((left._values != right._values).astype(int)) * 100.0 / len(left)
)
# error: Value of type variable "_Number" of "sum" cannot be
# "Union[ExtensionArray, ndarray, Any]"
thesum = np.sum(
(left._values != right._values).astype(int)
) # type: ignore[type-var]
# error: Unsupported operand types for * ("ExtensionArray" and "float")
diff = thesum * 100.0 / len(left) # type: ignore[operator]
msg = f"{obj} values are different ({np.round(diff, 5)} %)"
raise_assert_detail(obj, msg, left, right)
else:
Expand Down
10 changes: 8 additions & 2 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,17 @@

ArrayLike = Union["ExtensionArray", np.ndarray]
AnyArrayLike = Union[ArrayLike, "Index", "Series"]

AnySequenceLike = Union[
"ExtensionArray",
"Index",
"Series",
Sequence[Any],
np.ndarray,
]
# scalars

PythonScalar = Union[str, int, float, bool]
DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", "Period", "Timestamp", "Timedelta")
DatetimeLikeScalar = Union["Period", "Timestamp", "Timedelta"]
PandasScalar = Union["Period", "Timestamp", "Timedelta", "Interval"]
Scalar = Union[PythonScalar, PandasScalar]

Expand Down
14 changes: 9 additions & 5 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
)
from pandas._typing import (
AnyArrayLike,
AnySequenceLike,
ArrayLike,
Dtype,
DtypeObj,
FrameOrSeriesUnion,
)
Expand Down Expand Up @@ -216,7 +218,7 @@ def _ensure_data(values: ArrayLike) -> Tuple[np.ndarray, DtypeObj]:


def _reconstruct_data(
values: ArrayLike, dtype: DtypeObj, original: AnyArrayLike
values: ArrayLike, dtype: Dtype, original: AnyArrayLike
) -> ArrayLike:
"""
reverse of _ensure_data
Expand Down Expand Up @@ -446,7 +448,7 @@ def unique(values):
unique1d = unique


def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray:
def isin(comps: AnySequenceLike, values: AnySequenceLike) -> np.ndarray:
"""
Compute the isin boolean array.

Expand Down Expand Up @@ -482,9 +484,11 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray:
comps = _ensure_arraylike(comps)
comps = extract_array(comps, extract_numpy=True)
if is_extension_array_dtype(comps.dtype):
# error: Incompatible return value type (got "Series", expected "ndarray")
# error: Item "ndarray" of "Union[Any, ndarray]" has no attribute "isin"
return comps.isin(values) # type: ignore[return-value,union-attr]
# error: Argument 1 to "isin" of "ExtensionArray" has incompatible type
# "Union[Any, ExtensionArray, ndarray]"; expected "Sequence[Any]"
# error: Item "ndarray" of "Union[Any, ExtensionArray, ndarray]" has no
# attribute "isin"
return comps.isin(values) # type: ignore[arg-type, union-attr]

elif needs_i8_conversion(comps.dtype):
# Dispatch to DatetimeLikeArrayMixin.isin
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/array_algos/putmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def extract_bool_array(mask: ArrayLike) -> np.ndarray:
# We could have BooleanArray, Sparse[bool], ...
# Except for BooleanArray, this is equivalent to just
# np.asarray(mask, dtype=bool)
mask = mask.to_numpy(dtype=bool, na_value=False)
mask = mask.to_numpy(dtype=np.dtype(bool), na_value=False)

mask = np.asarray(mask, dtype=bool)
return mask
Expand Down
15 changes: 14 additions & 1 deletion pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from functools import wraps
from typing import (
Any,
List,
Optional,
Sequence,
Type,
TypeVar,
Union,
overload,
)

import numpy as np
Expand Down Expand Up @@ -253,8 +255,19 @@ def __setitem__(self, key, value):
def _validate_setitem_value(self, value):
return value

@overload
def __getitem__(self: NDArrayBackedExtensionArrayT, key: int) -> Any:
...

@overload
def __getitem__(
self: NDArrayBackedExtensionArrayT, key: Union[int, slice, np.ndarray]
self: NDArrayBackedExtensionArrayT, key: Union[slice, np.ndarray, List[Any]]
) -> NDArrayBackedExtensionArrayT:
...

def __getitem__(
self: NDArrayBackedExtensionArrayT,
key: Union[int, slice, np.ndarray, List[Any]],
) -> Union[NDArrayBackedExtensionArrayT, Any]:
if lib.is_integer(key):
# fast-path
Expand Down
Loading