-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REF: share _simple_new #37872
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
REF: share _simple_new #37872
Conversation
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.
lgtm
to begin with a quote from Fluent Python, p8
using type annotations in a runtime context is not something we've discussed. I think in general it could be fragile. we can only use with certain types so cannot now change cls._data for the subclasses without potentially breaking the code. So we wouldn't, for example, be able to makes Indexes generic, e.g. Categorical[str] if we used this pattern in other Indexes.
assuming that the assert has been added for runtime debugging and not to appease mypy: can you not add something akin to construct_array_type from ExtensionDtype as a class attribute to Index to store the backing array type?
is the reason type annotations for values has not been added related to the above? |
will do
I can add an annotation with |
right. so we want to so something like (completely untested!) (ArrayLikeT is the same as ArrayLike but we want to change that to a Union, and we want a typevar here)
|
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 @jbrockmendel lgtm pending green
pandas/core/indexes/base.py
Outdated
@@ -418,7 +418,7 @@ def asi8(self): | |||
return None | |||
|
|||
@classmethod | |||
def _simple_new(cls, values, name: Label = None): | |||
def _simple_new(cls, values: np.ndarray, name: Label = None): |
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.
because this is the base class it should be ArrayLike? (or we can leave this fix for #36092)
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.
no, its specifically np.ndarray
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 guess you want to make Index np.ndarray specific and have ExtensionIndex handle EA backed indexes but in that case some other type annotations in base would need to be changed e,g, _data: Union[ExtensionArray, np.ndarray]
so that methods in the base class that currently handle EA can be simplified and overridden.
In #37898, im getting error: Unsupported left operand type for - ("ExtensionArray")
in the base class since def _values(self) -> Union[ExtensionArray, np.ndarray]
so atm the base class is not EA agnostic.
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.
updated to get revert this annotation
def _simple_new( | ||
cls, | ||
values: Union[DatetimeArray, TimedeltaArray, PeriodArray], | ||
name: Label = None, |
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.
can now just use Hashable, see #37134
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.
OK, will edit on subsequent pass
updated + green |
@simonjayhawkins two questions: 1) is there a nice way to annotate the
values
arg (maybe a Protocol thing?) and 2) is the use ofcls.__annotations__["_data"]
an egregious antipattern?