From 81b425f4e0fff6f8828ac6f3fc85be727dc5d7a3 Mon Sep 17 00:00:00 2001 From: Matt Savoie Date: Fri, 2 Feb 2024 15:06:31 -0700 Subject: [PATCH] Move io tests, remove undefined reference to documentation. Also starts fixing simple mypy errors --- xarray/backends/common.py | 2 +- xarray/datatree_/datatree/io.py | 10 +-- xarray/datatree_/pyproject.toml | 61 ----------------- xarray/tests/datatree/conftest.py | 65 +++++++++++++++++++ .../tests => tests/datatree}/test_io.py | 6 +- 5 files changed, 76 insertions(+), 68 deletions(-) delete mode 100644 xarray/datatree_/pyproject.toml create mode 100644 xarray/tests/datatree/conftest.py rename xarray/{datatree_/datatree/tests => tests/datatree}/test_io.py (97%) diff --git a/xarray/backends/common.py b/xarray/backends/common.py index 8b585ca86a8..2e7d6a8f1aa 100644 --- a/xarray/backends/common.py +++ b/xarray/backends/common.py @@ -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`. + . Attributes ---------- diff --git a/xarray/datatree_/datatree/io.py b/xarray/datatree_/datatree/io.py index b460224a697..1b2e328c440 100644 --- a/xarray/datatree_/datatree/io.py +++ b/xarray/datatree_/datatree/io.py @@ -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 @@ -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) @@ -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( diff --git a/xarray/datatree_/pyproject.toml b/xarray/datatree_/pyproject.toml deleted file mode 100644 index 40f7d5a59b3..00000000000 --- a/xarray/datatree_/pyproject.toml +++ /dev/null @@ -1,61 +0,0 @@ -[project] -name = "xarray-datatree" -description = "Hierarchical tree-like data structures for xarray" -readme = "README.md" -authors = [ - {name = "Thomas Nicholas", email = "thomas.nicholas@columbia.edu"} -] -license = {text = "Apache-2"} -classifiers = [ - "Development Status :: 3 - Alpha", - "Intended Audience :: Science/Research", - "Topic :: Scientific/Engineering", - "License :: OSI Approved :: Apache Software License", - "Operating System :: OS Independent", - "Programming Language :: Python", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", -] -requires-python = ">=3.9" -dependencies = [ - "xarray >=2023.12.0", - "packaging", -] -dynamic = ["version"] - -[project.urls] -Home = "https://github.com/xarray-contrib/datatree" -Documentation = "https://xarray-datatree.readthedocs.io/en/stable/" - -[build-system] -requires = [ - "setuptools>=61.0.0", - "wheel", - "setuptools_scm[toml]>=7.0", - "check-manifest" -] - -[tool.setuptools_scm] -write_to = "datatree/_version.py" -write_to_template = ''' -# Do not change! Do not track in version control! -__version__ = "{version}" -''' - -[tool.setuptools.packages.find] -exclude = ["docs", "tests", "tests.*", "docs.*"] - -[tool.setuptools.package-data] -datatree = ["py.typed"] - -[tool.isort] -profile = "black" -skip_gitignore = true -float_to_top = true -default_section = "THIRDPARTY" -known_first_party = "datatree" - -[mypy] -files = "datatree/**/*.py" -show_error_codes = true diff --git a/xarray/tests/datatree/conftest.py b/xarray/tests/datatree/conftest.py new file mode 100644 index 00000000000..d3593ff36de --- /dev/null +++ b/xarray/tests/datatree/conftest.py @@ -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: + + + |-- set1 + | |-- + | | Dimensions: () + | | Data variables: + | | a int64 0 + | | b int64 1 + | |-- set1 + | |-- set2 + |-- set2 + | |-- + | | Dimensions: (x: 2) + | | Data variables: + | | a (x) int64 2, 3 + | | b (x) int64 0.1, 0.2 + | |-- set1 + |-- set3 + |-- + | 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() diff --git a/xarray/datatree_/datatree/tests/test_io.py b/xarray/tests/datatree/test_io.py similarity index 97% rename from xarray/datatree_/datatree/tests/test_io.py rename to xarray/tests/datatree/test_io.py index e2744b14a2d..fef94c9fbb2 100644 --- a/xarray/datatree_/datatree/tests/test_io.py +++ b/xarray/tests/datatree/test_io.py @@ -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: