-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Use a bound TypeVar
for DataArray
and Dataset
methods
#8208
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6177f6d
Use a bound `TypeVar` for `DataArray` and `Dataset` methods
max-sixty ea72ad7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 897e8e0
1ab9210
e1c3df1
ef42745
Update xarray/core/common.py
max-sixty File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
Generic, | ||
Literal, | ||
Union, | ||
cast, | ||
) | ||
|
||
import numpy as np | ||
|
@@ -256,7 +257,7 @@ def to_dataarray(self) -> DataArray: | |
|
||
def _ensure_1d( | ||
group: T_Group, obj: T_Xarray | ||
) -> tuple[T_Group, T_Xarray, Hashable | None, list[Hashable],]: | ||
) -> tuple[T_Group, T_Xarray, str | None, list[Hashable]]: | ||
# 1D cases: do nothing | ||
if isinstance(group, (IndexVariable, _DummyGroup)) or group.ndim == 1: | ||
return group, obj, None, [] | ||
|
@@ -271,7 +272,7 @@ def _ensure_1d( | |
inserted_dims = [dim for dim in group.dims if dim not in group.coords] | ||
newgroup = group.stack({stacked_dim: orig_dims}) | ||
newobj = obj.stack({stacked_dim: orig_dims}) | ||
return newgroup, newobj, stacked_dim, inserted_dims | ||
return newgroup, cast(T_Xarray, newobj), stacked_dim, inserted_dims | ||
|
||
raise TypeError( | ||
f"group must be DataArray, IndexVariable or _DummyGroup, got {type(group)!r}." | ||
|
@@ -800,7 +801,10 @@ def __getitem__(self, key: GroupKey) -> T_Xarray: | |
""" | ||
Get DataArray or Dataset corresponding to a particular group label. | ||
""" | ||
return self._obj.isel({self._group_dim: self.groups[key]}) | ||
return cast( | ||
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. That's weird that these casts are now necessary... |
||
T_Xarray, | ||
self._obj.isel(indexers={self._group_dim: self.groups[key]}), | ||
) | ||
|
||
def __len__(self) -> int: | ||
(grouper,) = self.groupers | ||
|
@@ -822,7 +826,7 @@ def __repr__(self) -> str: | |
def _iter_grouped(self) -> Iterator[T_Xarray]: | ||
"""Iterate over each element in this group""" | ||
for indices in self._group_indices: | ||
yield self._obj.isel({self._group_dim: indices}) | ||
yield cast(T_Xarray, self._obj.isel({self._group_dim: indices})) | ||
|
||
def _infer_concat_args(self, applied_example): | ||
(grouper,) = self.groupers | ||
|
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Unfortunately this doesn't work, similar to the reason outlined below —
T_Xarray
isn't compatible withT_DataWithCoords
...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 think thats why originally I was typing RollingExp using T_DataWithCoords