-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: Arraylike #31574
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
TYP: Arraylike #31574
Changes from 18 commits
54f2c52
b339215
60567d2
1ec8da0
f01f4b1
7212b5a
5f5231d
42a178f
a6cc70c
ead6793
9f03c51
3db3dca
f7fca6e
c987bc2
384ac32
6984eaf
c0772a2
074bbd4
f6db38f
0b66334
cdb39f2
1ba670d
ff2ff9c
3d6f7a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,12 @@ | |
|
||
import builtins | ||
import textwrap | ||
from typing import Any, Dict, FrozenSet, List, Optional, Union | ||
from typing import Any, Dict, FrozenSet, Generic, List, Optional | ||
|
||
import numpy as np | ||
|
||
import pandas._libs.lib as lib | ||
from pandas._typing import ArrayLike | ||
from pandas.compat import PYPY | ||
from pandas.compat.numpy import function as nv | ||
from pandas.errors import AbstractMethodError | ||
|
@@ -584,7 +585,7 @@ def _shallow_copy(self, obj, **kwargs): | |
return self._constructor(obj, **kwargs) | ||
|
||
|
||
class IndexOpsMixin: | ||
class IndexOpsMixin(Generic[ArrayLike]): | ||
""" | ||
Common ops mixin to support a unified interface / docs for Series / Index | ||
""" | ||
|
@@ -596,7 +597,7 @@ class IndexOpsMixin: | |
) | ||
|
||
@property | ||
def _values(self) -> Union[ExtensionArray, np.ndarray]: | ||
def _values(self) -> ArrayLike: | ||
# must be defined here as a property for mypy | ||
raise AbstractMethodError(self) | ||
|
||
|
@@ -1141,7 +1142,10 @@ def _map_values(self, mapper, na_action=None): | |
values = self._values | ||
if na_action is not None: | ||
raise NotImplementedError | ||
map_f = lambda values, f: values.map(f) | ||
|
||
def map_f(values, f): | ||
return values.map(f) | ||
|
||
Comment on lines
+1145
to
+1148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is changed to silence |
||
else: | ||
values = self.astype(object) | ||
values = getattr(values, "values", values) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,7 +230,10 @@ def _simple_new(cls, values: PeriodArray, name: Label = None): | |
Values that can be converted to a PeriodArray without inference | ||
or coercion. | ||
""" | ||
assert isinstance(values, PeriodArray), type(values) | ||
if not isinstance(values, PeriodArray): | ||
raise TypeError( | ||
f"_simple_new expects PeriodArray, got {type(values).__name__}" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think an assertion is correct here since this is internal and annotated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
we have tests to test these. |
||
|
||
result = object.__new__(cls) | ||
result._data = values | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,7 +321,9 @@ def test_constructor_mixed(self): | |
def test_constructor_simple_new(self): | ||
idx = period_range("2007-01", name="p", periods=2, freq="M") | ||
|
||
with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"): | ||
with pytest.raises( | ||
TypeError, match="_simple_new expects PeriodArray, got PeriodIndex" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: i prefer |
||
): | ||
idx._simple_new(idx, name="p") | ||
|
||
result = idx._simple_new(idx._data, name="p") | ||
|
@@ -339,15 +341,21 @@ def test_constructor_simple_new(self): | |
def test_constructor_simple_new_empty(self): | ||
# GH13079 | ||
idx = PeriodIndex([], freq="M", name="p") | ||
with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"): | ||
with pytest.raises( | ||
TypeError, match="_simple_new expects PeriodArray, got PeriodIndex" | ||
): | ||
idx._simple_new(idx, name="p") | ||
|
||
result = idx._simple_new(idx._data, name="p") | ||
tm.assert_index_equal(result, idx) | ||
|
||
@pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])]) | ||
def test_constructor_floats(self, floats): | ||
with pytest.raises(AssertionError, match="<class "): | ||
@pytest.mark.parametrize( | ||
"floats,box", [([1.1, 2.1], "list"), (np.array([1.1, 2.1]), "ndarray")] | ||
) | ||
def test_constructor_floats(self, floats, box): | ||
with pytest.raises( | ||
TypeError, match=f"_simple_new expects PeriodArray, got {box}" | ||
): | ||
PeriodIndex._simple_new(floats) | ||
|
||
msg = "PeriodIndex does not allow floating point in construction" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I wouldn't really consider a Mixin to be a Generic class - have you seen this pattern used elsewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see errors in OP of #31518
The goal here is to use the ArrayLike alias from pandas._typing to replace uses of
Union[ExtensionArray, np.ndarray]
. ArrayLike is a typevar and hence mypy reports errors if the typevar is not bound.In Index we want the return type of _values to be the same type as self._data. The way to bind typevars within a class is to use Generic.
More value would come from having the correct type available in calling code instead of a Union type to reduce casts, asserts and type ignores to perform narrowing.
I'll mark this as draft till this is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. So my question was more around the general approach of dealing with Mixins here. The mypy suggested approach I think is using Protocol:
https://mypy.readthedocs.io/en/stable/more_types.html#mixin-classes
So just curious if you found this approach elsewhere in the docs or are using another project as a precedent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from those docs
At the moment
_values
has already been added (not in this PR) as an abstract method on L680 so changing to Protocol is orthogonal to this PR.PEP 544 states
we can have the discussion regarding the use of protocol and the migration path in #33274
This PR is about binding Typvars.
In common with the Generic abc, Protocol is generic and would still need the typevar in the (Protocol) definition class...
(example from PEP 544)
so if we adopt Protocols in the future we would use
SomeClassWithJustTheAbstractMethods[Protocol[ArrayLike]]
instead ofIndexOpsMixin(Generic[ArrayLike])
with the abstract methods included.hopefully answered as not relevant.
following advice from mypy
pandas/core/indexes/base.py:246: note: (Hint: Use "Generic[ArrayLike]" or "Protocol[ArrayLike]" base class to bind "ArrayLike" inside a class)