Skip to content

Commit

Permalink
Merge branch 'main' into groupby-save-codes-new
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian authored Mar 2, 2023
2 parents ea3ed87 + e04109f commit 2d3f984
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repos:
files: ^xarray/
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.248'
rev: 'v0.0.253'
hooks:
- id: ruff
args: ["--fix"]
Expand Down
4 changes: 2 additions & 2 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def _get_zarr_dims_and_attrs(zarr_obj, dimension_key, try_nczarr):
"which are required for xarray to determine variable dimensions."
) from e

nc_attrs = [attr for attr in zarr_obj.attrs if attr.startswith("_NC")]
nc_attrs = [attr for attr in zarr_obj.attrs if attr.lower().startswith("_nc")]
attributes = HiddenKeyDict(zarr_obj.attrs, [dimension_key] + nc_attrs)
return dimensions, attributes

Expand Down Expand Up @@ -495,7 +495,7 @@ def get_attrs(self):
return {
k: v
for k, v in self.zarr_group.attrs.asdict().items()
if not k.startswith("_NC")
if not k.lower().startswith("_nc")
}

def get_dimensions(self):
Expand Down
13 changes: 3 additions & 10 deletions xarray/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Any,
Callable,
Literal,
Protocol,
SupportsIndex,
TypeVar,
Union,
Expand All @@ -18,6 +17,7 @@
from packaging.version import Version

if TYPE_CHECKING:
from numpy._typing import _SupportsDType
from numpy.typing import ArrayLike

from xarray.backends.common import BackendEntrypoint
Expand Down Expand Up @@ -50,19 +50,12 @@
_ShapeLike = Union[SupportsIndex, Sequence[SupportsIndex]]
_DTypeLikeNested = Any # TODO: wait for support for recursive types

# once NumPy 1.21 is minimum version, use NumPys definition directly
# 1.20 uses a non-generic Protocol (like we define here for simplicity)
class _SupportsDType(Protocol):
@property
def dtype(self) -> np.dtype:
...

# Xarray requires a Mapping[Hashable, dtype] in many places which
# conflics with numpys own DTypeLike (with dtypes for fields).
# https://numpy.org/devdocs/reference/typing.html#numpy.typing.DTypeLike
# This is a copy of this DTypeLike that allows only non-Mapping dtypes.
DTypeLikeSave = Union[
np.dtype,
np.dtype[Any],
# default data type (float64)
None,
# array-scalar types and generic types
Expand All @@ -78,7 +71,7 @@ def dtype(self) -> np.dtype:
# because numpy does the same?
list[Any],
# anything with a dtype attribute
_SupportsDType,
_SupportsDType[np.dtype[Any]],
]
try:
from cftime import datetime as CFTimeDatetime
Expand Down
19 changes: 11 additions & 8 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5663,12 +5663,14 @@ def test_write_file_from_np_str(str_type, tmpdir) -> None:
@requires_zarr
@requires_netCDF4
class TestNCZarr:
@staticmethod
def _create_nczarr(filename):
netcdfc_version = Version(nc4.getlibversion().split()[0])
if netcdfc_version < Version("4.8.1"):
@property
def netcdfc_version(self):
return Version(nc4.getlibversion().split()[0])

def _create_nczarr(self, filename):
if self.netcdfc_version < Version("4.8.1"):
pytest.skip("requires netcdf-c>=4.8.1")
if (platform.system() == "Windows") and (netcdfc_version == Version("4.8.1")):
if platform.system() == "Windows" and self.netcdfc_version == Version("4.8.1"):
# Bug in netcdf-c==4.8.1 (typo: Nan instead of NaN)
# https://github.com/Unidata/netcdf-c/issues/2265
pytest.skip("netcdf-c==4.8.1 has issues on Windows")
Expand All @@ -5678,9 +5680,7 @@ def _create_nczarr(filename):
# https://github.com/Unidata/netcdf-c/issues/2259
ds = ds.drop_vars("dim3")

# netcdf-c>4.8.1 will add _ARRAY_DIMENSIONS by default
mode = "nczarr" if netcdfc_version == Version("4.8.1") else "nczarr,noxarray"
ds.to_netcdf(f"file://{filename}#mode={mode}")
ds.to_netcdf(f"file://{filename}#mode=nczarr")
return ds

def test_open_nczarr(self) -> None:
Expand All @@ -5700,6 +5700,9 @@ def test_overwriting_nczarr(self) -> None:
@pytest.mark.parametrize("mode", ["a", "r+"])
@pytest.mark.filterwarnings("ignore:.*non-consolidated metadata.*")
def test_raise_writing_to_nczarr(self, mode) -> None:
if self.netcdfc_version > Version("4.8.1"):
pytest.skip("netcdf-c>4.8.1 adds the _ARRAY_DIMENSIONS attribute")

with create_tmp_file(suffix=".zarr") as tmp:
ds = self._create_nczarr(tmp)
with pytest.raises(
Expand Down

0 comments on commit 2d3f984

Please sign in to comment.