-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: _concat_same_type method of EA #37817
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
Changes from 1 commit
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import operator | ||
from operator import le, lt | ||
import textwrap | ||
from typing import TYPE_CHECKING, Optional, Tuple, Union, cast | ||
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Type, TypeVar, Union, cast | ||
|
||
import numpy as np | ||
|
||
|
@@ -56,6 +56,8 @@ | |
from pandas import Index | ||
from pandas.core.arrays import DatetimeArray, TimedeltaArray | ||
|
||
IntervalArrayT = TypeVar("IntervalArrayT", bound="IntervalArray") | ||
|
||
_interval_shared_docs = {} | ||
|
||
_shared_docs_kwargs = dict( | ||
|
@@ -722,7 +724,9 @@ def equals(self, other) -> bool: | |
) | ||
|
||
@classmethod | ||
def _concat_same_type(cls, to_concat): | ||
def _concat_same_type( | ||
cls: Type[IntervalArrayT], to_concat: Sequence[IntervalArrayT] | ||
) -> IntervalArrayT: | ||
""" | ||
Concatenate multiple IntervalArray | ||
|
||
|
@@ -1468,10 +1472,19 @@ def _get_combined_data( | |
axis=1, | ||
) | ||
else: | ||
left = cast(Union["DatetimeArray", "TimedeltaArray"], left) | ||
right = cast(Union["DatetimeArray", "TimedeltaArray"], right) | ||
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. the casting doesnt help? 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. the casts as they stand give
and would need to be changed. I can't immediately see what guarentees we have that justify the casts. the 'ignore' is a fix later properly. (I guess the function signature here maybe incorrect, but not checked) and will circle back to this after getting started on the other EA methods as a precursor to adding type annotations to #35259 If this is not a false positive, it needs more investigation before casting to silence the mypy errors. |
||
combined = type(left)._concat_same_type( | ||
[left.reshape(-1, 1), right.reshape(-1, 1)], | ||
# error: Item "type" of "Union[Type[Index], Type[ExtensionArray]]" has | ||
# no attribute "_concat_same_type" [union-attr] | ||
|
||
# error: Unexpected keyword argument "axis" for "_concat_same_type" of | ||
# "ExtensionArray" [call-arg] | ||
|
||
# error: Item "Index" of "Union[Index, ExtensionArray]" has no | ||
# attribute "reshape" [union-attr] | ||
|
||
# error: Item "ExtensionArray" of "Union[Index, ExtensionArray]" has no | ||
# attribute "reshape" [union-attr] | ||
combined = type(left)._concat_same_type( # type: ignore[union-attr,call-arg] | ||
[left.reshape(-1, 1), right.reshape(-1, 1)], # type: ignore[union-attr] | ||
axis=1, | ||
) | ||
return combined |
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.
I might have asked before, but can you explain again why this is needed, and the
"ExtensionArray"
as string version doesn't work?(I think I mentioned this before, but IMO we should have better documentation about the specific patterns we are using in pandas in our contributing guides)
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.
these are not specific patterns to pandas. https://mypy.readthedocs.io/en/stable/generics.html#generic-functions and https://mypy.readthedocs.io/en/stable/generics.html#generic-methods-and-generic-self
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.
I do recall explaining it in the past (obviously didn't do a good job) won't bother with a generic explanation again, and next time I come across a concrete example of why this is needed will cc you in.