Declare Dataset, DataArray, Variable, GroupBy unhashable#8392
Declare Dataset, DataArray, Variable, GroupBy unhashable#8392max-sixty merged 3 commits intopydata:mainfrom
Conversation
efbc043 to
ec1b67e
Compare
xarray/core/_typed_ops.py
Outdated
| def __eq__(self, other: VarCompatible) -> Self | T_DataArray: | ||
| return self._binary_op(other, nputils.array_eq) | ||
|
|
||
| __hash__: None # type:ignore[assignment] |
There was a problem hiding this comment.
Good for testing but FYI:
xarray/xarray/core/_typed_ops.py
Line 2 in ec1b67e
|
This looks good! CC @headtr1ck & @Illviljan . I have a faint memory of issues with We'll need to move the |
|
I'm done for the day, and I'm not sure when I can come back to this, so if someone else is motivated to update the script and get this done, then please don't wait for me. |
2bc7ef6 to
81bde70
Compare
The previous commit revealed the following mypy error: xarray/core/dataset.py: note: In member "swap_dims" of class "Dataset": xarray/core/dataset.py:4415: error: Incompatible types in assignment (expression has type "Variable", variable has type "Hashable") [assignment] xarray/core/dataset.py:4415: note: Following member(s) of "Variable" have conflicts: xarray/core/dataset.py:4415: note: __hash__: expected "Callable[[], int]", got "None" xarray/core/dataset.py:4416: error: "Hashable" has no attribute "dims" [attr-defined] xarray/core/dataset.py:4419: error: "Hashable" has no attribute "to_index_variable" [attr-defined] xarray/core/dataset.py:4430: error: "Hashable" has no attribute "to_base_variable" [attr-defined]
81bde70 to
f28b735
Compare
|
@max-sixty, I think I got it via the template. Ready for another round of review. I verified that the mypy diff from the regenerate commit is empty. |
| var = current_variable.to_base_variable() | ||
| var.dims = dims | ||
| variables[k] = var | ||
| variables[current_name] = var |
There was a problem hiding this comment.
Thanks, these are good renames!
|
Thank you @maresb ! |
* main: [skip-ci] dev whats-new (pydata#8467) 2023.11.0 Whats-new (pydata#8461) migrate the other CI to python 3.11 (pydata#8416) preserve vlen string dtypes, allow vlen string fill_values (pydata#7869) Pin mypy < 1.7 (pydata#8458) Fix typos found by codespell (pydata#8457) [skip-ci] Small updates to IO docs. (pydata#8452) Deprecate certain cftime frequency strings following pandas (pydata#8415) Added driver parameter for h5netcdf (pydata#8360) Raise exception in to_dataset if resulting variable is also the name of a coordinate (pydata#8433) Automatic region detection and transpose for `to_zarr()` (pydata#8434) remove `cdms2` (pydata#8441) Remove PseudoNetCDF (pydata#8446) Pin pint to >=0.22 (pydata#8445) Remove keep_attrs from resample signature (pydata#8444) Rename `to_array` to `to_dataarray` (pydata#8438) Add missing DataArray.dt.total_seconds() method (pydata#8435) Declare Dataset, DataArray, Variable, GroupBy unhashable (pydata#8392)
Declare
Dataset,DataArray,Variable,DatasetGroupBy, andDataArrayGroupByunhashable by adding the following type declaration to each class's OpsMixin class:Background
This is a follow-up to #8384 (attn: @max-sixty). There I observed that pyright was objecting that
Variableis not a valid type to thedimargument ofconcat. On the other hand mypy accepted this because mypy recognizesVariableasHashableeven though it is not.The appropriate place to do declare
Variableunhashable seems to be beside the definition of__eq__inVariableOpsMixinsince defining__eq__without__hash__is what renders an object unhashable.For a reference to how types are marked as non-hashable, see https://github.com/python/typeshed/pull/3219/files.
After making this change, mypy reported the following additional error:
This error was the result of a variable redefinition in
Dataset.swap_dims. Giving the variables distinct (and more descriptive) names resolved the error.