-
-
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
Closed
Closed
TYP: Arraylike #31574
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
54f2c52
TYP: Arraylike
jbrockmendel b339215
Merge remote-tracking branch 'upstream/master' into arraylike
simonjayhawkins 60567d2
mypy fixup
simonjayhawkins 1ec8da0
Merge remote-tracking branch 'upstream/master' into arraylike
simonjayhawkins f01f4b1
mypy/isort fixup
simonjayhawkins 7212b5a
Merge remote-tracking branch 'upstream/master' into arraylike
simonjayhawkins 5f5231d
mypy fixup
simonjayhawkins 42a178f
revert add itemsize to EA interface
simonjayhawkins a6cc70c
revert changes to pytables
simonjayhawkins ead6793
Merge remote-tracking branch 'upstream/master' into arraylike
simonjayhawkins 9f03c51
Merge remote-tracking branch 'upstream/master' into arraylike
simonjayhawkins 3db3dca
Merge remote-tracking branch 'upstream/master' into arraylike
simonjayhawkins f7fca6e
unsed classes following merge upsteam
simonjayhawkins c987bc2
whitespace
simonjayhawkins 384ac32
troubleshoot test failure
simonjayhawkins 6984eaf
restore replace lambda
simonjayhawkins c0772a2
AssertionError -> TypeError
simonjayhawkins 074bbd4
more test changes
simonjayhawkins f6db38f
Merge remote-tracking branch 'upstream/master' into arraylike
simonjayhawkins 0b66334
revert changes to 'debugging' asserts that were tested
simonjayhawkins cdb39f2
fixup tests
simonjayhawkins 1ba670d
Merge remote-tracking branch 'upstream/master' into arraylike
simonjayhawkins ff2ff9c
Merge remote-tracking branch 'upstream/master' into arraylike
simonjayhawkins 3d6f7a6
fix doc failures
simonjayhawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
if na_action == "ignore": | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)