Skip to content

Commit

Permalink
Move io tests, remove undefined reference to documentation.
Browse files Browse the repository at this point in the history
Also starts fixing simple mypy errors
  • Loading branch information
flamingbear committed Feb 3, 2024
1 parent 77405d9 commit 81b425f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 68 deletions.
2 changes: 1 addition & 1 deletion xarray/backends/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ class BackendEntrypoint:
decoding and it returns an instance of :py:class:`~datatree.DataTree`.
It shall take in input at least ``filename_or_obj`` argument. The
implementation of this method is not mandatory. For more details see
:ref:`RST open_datatree`.
<reference to open_datatree documentation>.
Attributes
----------
Expand Down
10 changes: 5 additions & 5 deletions xarray/datatree_/datatree/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

def _get_nc_dataset_class(engine):
if engine == "netcdf4":
from netCDF4 import Dataset # type: ignore
from netCDF4 import Dataset
elif engine == "h5netcdf":
from h5netcdf.legacyapi import Dataset # type: ignore
from h5netcdf.legacyapi import Dataset
elif engine is None:
try:
from netCDF4 import Dataset
except ImportError:
from h5netcdf.legacyapi import Dataset # type: ignore
from h5netcdf.legacyapi import Dataset
else:
raise ValueError(f"unsupported engine: {engine}")
return Dataset
Expand Down Expand Up @@ -78,7 +78,7 @@ def _datatree_to_netcdf(


def _create_empty_zarr_group(store, group, mode):
import zarr # type: ignore
import zarr

root = zarr.open_group(store, mode=mode)
root.create_group(group, overwrite=True)
Expand All @@ -92,7 +92,7 @@ def _datatree_to_zarr(
consolidated: bool = True,
**kwargs,
):
from zarr.convenience import consolidate_metadata # type: ignore
from zarr.convenience import consolidate_metadata

if kwargs.get("group", None) is not None:
raise NotImplementedError(
Expand Down
61 changes: 0 additions & 61 deletions xarray/datatree_/pyproject.toml

This file was deleted.

65 changes: 65 additions & 0 deletions xarray/tests/datatree/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import pytest

import xarray as xr
from xarray.datatree_.datatree import DataTree


@pytest.fixture(scope="module")
def create_test_datatree():
"""
Create a test datatree with this structure:
<datatree.DataTree>
|-- set1
| |-- <xarray.Dataset>
| | Dimensions: ()
| | Data variables:
| | a int64 0
| | b int64 1
| |-- set1
| |-- set2
|-- set2
| |-- <xarray.Dataset>
| | Dimensions: (x: 2)
| | Data variables:
| | a (x) int64 2, 3
| | b (x) int64 0.1, 0.2
| |-- set1
|-- set3
|-- <xarray.Dataset>
| Dimensions: (x: 2, y: 3)
| Data variables:
| a (y) int64 6, 7, 8
| set0 (x) int64 9, 10
The structure has deliberately repeated names of tags, variables, and
dimensions in order to better check for bugs caused by name conflicts.
"""

def _create_test_datatree(modify=lambda ds: ds):
set1_data = modify(xr.Dataset({"a": 0, "b": 1}))
set2_data = modify(xr.Dataset({"a": ("x", [2, 3]), "b": ("x", [0.1, 0.2])}))
root_data = modify(xr.Dataset({"a": ("y", [6, 7, 8]), "set0": ("x", [9, 10])}))

# Avoid using __init__ so we can independently test it
root = DataTree(data=root_data)
set1 = DataTree(name="set1", parent=root, data=set1_data)
DataTree(name="set1", parent=set1)
DataTree(name="set2", parent=set1)
set2 = DataTree(name="set2", parent=root, data=set2_data)
DataTree(name="set1", parent=set2)
DataTree(name="set3", parent=root)

return root

return _create_test_datatree


@pytest.fixture(scope="module")
def simple_datatree(create_test_datatree):
"""
Invoke create_test_datatree fixture (callback).
Returns a DataTree.
"""
return create_test_datatree()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

from xarray.backends.api import open_datatree
from xarray.datatree_.datatree.testing import assert_equal
from xarray.datatree_.datatree.tests import requires_h5netcdf, requires_netCDF4, requires_zarr
from xarray.tests import (
requires_h5netcdf,
requires_netCDF4,
requires_zarr,
)


class TestIO:
Expand Down

0 comments on commit 81b425f

Please sign in to comment.