Skip to content

Commit 11542fb

Browse files
Revert "Migrate datatree io.py and common.py into xarray/core (#9011)"
This reverts commit cb3663d.
1 parent cb3663d commit 11542fb

File tree

9 files changed

+152
-150
lines changed

9 files changed

+152
-150
lines changed

doc/whats-new.rst

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ What's New
1717
1818
.. _whats-new.2024.05.1:
1919

20-
v2024.06 (unreleased)
20+
v2024.05.1 (unreleased)
2121
-----------------------
2222

2323
New Features
@@ -59,10 +59,6 @@ Documentation
5959

6060
Internal Changes
6161
~~~~~~~~~~~~~~~~
62-
- Migrates remainder of ``io.py`` to ``xarray/core/datatree_io.py`` and
63-
``TreeAttrAccessMixin`` into ``xarray/core/common.py`` (:pull: `9011`)
64-
By `Owen Littlejohns <https://github.com/owenlittlejohns>`_ and
65-
`Tom Nicholas <https://github.com/TomNicholas>`_.
6662

6763

6864
.. _whats-new.2024.05.0:
@@ -145,9 +141,10 @@ Internal Changes
145141
By `Owen Littlejohns <https://github.com/owenlittlejohns>`_, `Matt Savoie
146142
<https://github.com/flamingbear>`_ and `Tom Nicholas <https://github.com/TomNicholas>`_.
147143
- ``transpose``, ``set_dims``, ``stack`` & ``unstack`` now use a ``dim`` kwarg
148-
rather than ``dims`` or ``dimensions``. This is the final change to make xarray methods
149-
consistent with their use of ``dim``. Using the existing kwarg will raise a
150-
warning. By `Maximilian Roos <https://github.com/max-sixty>`_
144+
rather than ``dims`` or ``dimensions``. This is the final change to unify
145+
xarray functions to use ``dim``. Using the existing kwarg will raise a
146+
warning.
147+
By `Maximilian Roos <https://github.com/max-sixty>`_
151148

152149
.. _whats-new.2024.03.0:
153150

xarray/backends/api.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from xarray.core.dataarray import DataArray
3737
from xarray.core.dataset import Dataset, _get_chunk, _maybe_chunk
3838
from xarray.core.indexes import Index
39-
from xarray.core.types import NetcdfWriteModes, ZarrWriteModes
39+
from xarray.core.types import ZarrWriteModes
4040
from xarray.core.utils import is_remote_uri
4141
from xarray.namedarray.daskmanager import DaskManager
4242
from xarray.namedarray.parallelcompat import guess_chunkmanager
@@ -1120,7 +1120,7 @@ def open_mfdataset(
11201120
def to_netcdf(
11211121
dataset: Dataset,
11221122
path_or_file: str | os.PathLike | None = None,
1123-
mode: NetcdfWriteModes = "w",
1123+
mode: Literal["w", "a"] = "w",
11241124
format: T_NetcdfTypes | None = None,
11251125
group: str | None = None,
11261126
engine: T_NetcdfEngine | None = None,
@@ -1138,7 +1138,7 @@ def to_netcdf(
11381138
def to_netcdf(
11391139
dataset: Dataset,
11401140
path_or_file: None = None,
1141-
mode: NetcdfWriteModes = "w",
1141+
mode: Literal["w", "a"] = "w",
11421142
format: T_NetcdfTypes | None = None,
11431143
group: str | None = None,
11441144
engine: T_NetcdfEngine | None = None,
@@ -1155,7 +1155,7 @@ def to_netcdf(
11551155
def to_netcdf(
11561156
dataset: Dataset,
11571157
path_or_file: str | os.PathLike,
1158-
mode: NetcdfWriteModes = "w",
1158+
mode: Literal["w", "a"] = "w",
11591159
format: T_NetcdfTypes | None = None,
11601160
group: str | None = None,
11611161
engine: T_NetcdfEngine | None = None,
@@ -1173,7 +1173,7 @@ def to_netcdf(
11731173
def to_netcdf(
11741174
dataset: Dataset,
11751175
path_or_file: str | os.PathLike,
1176-
mode: NetcdfWriteModes = "w",
1176+
mode: Literal["w", "a"] = "w",
11771177
format: T_NetcdfTypes | None = None,
11781178
group: str | None = None,
11791179
engine: T_NetcdfEngine | None = None,
@@ -1191,7 +1191,7 @@ def to_netcdf(
11911191
def to_netcdf(
11921192
dataset: Dataset,
11931193
path_or_file: str | os.PathLike,
1194-
mode: NetcdfWriteModes = "w",
1194+
mode: Literal["w", "a"] = "w",
11951195
format: T_NetcdfTypes | None = None,
11961196
group: str | None = None,
11971197
engine: T_NetcdfEngine | None = None,
@@ -1209,7 +1209,7 @@ def to_netcdf(
12091209
def to_netcdf(
12101210
dataset: Dataset,
12111211
path_or_file: str | os.PathLike,
1212-
mode: NetcdfWriteModes = "w",
1212+
mode: Literal["w", "a"] = "w",
12131213
format: T_NetcdfTypes | None = None,
12141214
group: str | None = None,
12151215
engine: T_NetcdfEngine | None = None,
@@ -1226,7 +1226,7 @@ def to_netcdf(
12261226
def to_netcdf(
12271227
dataset: Dataset,
12281228
path_or_file: str | os.PathLike | None,
1229-
mode: NetcdfWriteModes = "w",
1229+
mode: Literal["w", "a"] = "w",
12301230
format: T_NetcdfTypes | None = None,
12311231
group: str | None = None,
12321232
engine: T_NetcdfEngine | None = None,
@@ -1241,7 +1241,7 @@ def to_netcdf(
12411241
def to_netcdf(
12421242
dataset: Dataset,
12431243
path_or_file: str | os.PathLike | None = None,
1244-
mode: NetcdfWriteModes = "w",
1244+
mode: Literal["w", "a"] = "w",
12451245
format: T_NetcdfTypes | None = None,
12461246
group: str | None = None,
12471247
engine: T_NetcdfEngine | None = None,

xarray/core/common.py

-18
Original file line numberDiff line numberDiff line change
@@ -347,24 +347,6 @@ def _ipython_key_completions_(self) -> list[str]:
347347
return list(items)
348348

349349

350-
class TreeAttrAccessMixin(AttrAccessMixin):
351-
"""Mixin class that allows getting keys with attribute access"""
352-
353-
# TODO: Ensure ipython tab completion can include both child datatrees and
354-
# variables from Dataset objects on relevant nodes.
355-
356-
__slots__ = ()
357-
358-
def __init_subclass__(cls, **kwargs):
359-
"""This method overrides the check from ``AttrAccessMixin`` that ensures
360-
``__dict__`` is absent in a class, with ``__slots__`` used instead.
361-
``DataTree`` has some dynamically defined attributes in addition to those
362-
defined in ``__slots__``. (GH9068)
363-
"""
364-
if not hasattr(object.__new__(cls), "__dict__"):
365-
pass
366-
367-
368350
def get_squeeze_dims(
369351
xarray_obj,
370352
dim: Hashable | Iterable[Hashable] | None = None,

xarray/core/dataarray.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
from xarray.core.options import OPTIONS, _get_keep_attrs
5555
from xarray.core.types import (
5656
DaCompatible,
57-
NetcdfWriteModes,
5857
T_DataArray,
5958
T_DataArrayOrSet,
6059
ZarrWriteModes,
@@ -3946,7 +3945,7 @@ def to_masked_array(self, copy: bool = True) -> np.ma.MaskedArray:
39463945
def to_netcdf(
39473946
self,
39483947
path: None = None,
3949-
mode: NetcdfWriteModes = "w",
3948+
mode: Literal["w", "a"] = "w",
39503949
format: T_NetcdfTypes | None = None,
39513950
group: str | None = None,
39523951
engine: T_NetcdfEngine | None = None,
@@ -3961,7 +3960,7 @@ def to_netcdf(
39613960
def to_netcdf(
39623961
self,
39633962
path: str | PathLike,
3964-
mode: NetcdfWriteModes = "w",
3963+
mode: Literal["w", "a"] = "w",
39653964
format: T_NetcdfTypes | None = None,
39663965
group: str | None = None,
39673966
engine: T_NetcdfEngine | None = None,
@@ -3977,7 +3976,7 @@ def to_netcdf(
39773976
def to_netcdf(
39783977
self,
39793978
path: str | PathLike,
3980-
mode: NetcdfWriteModes = "w",
3979+
mode: Literal["w", "a"] = "w",
39813980
format: T_NetcdfTypes | None = None,
39823981
group: str | None = None,
39833982
engine: T_NetcdfEngine | None = None,
@@ -3993,7 +3992,7 @@ def to_netcdf(
39933992
def to_netcdf(
39943993
self,
39953994
path: str | PathLike,
3996-
mode: NetcdfWriteModes = "w",
3995+
mode: Literal["w", "a"] = "w",
39973996
format: T_NetcdfTypes | None = None,
39983997
group: str | None = None,
39993998
engine: T_NetcdfEngine | None = None,
@@ -4006,7 +4005,7 @@ def to_netcdf(
40064005
def to_netcdf(
40074006
self,
40084007
path: str | PathLike | None = None,
4009-
mode: NetcdfWriteModes = "w",
4008+
mode: Literal["w", "a"] = "w",
40104009
format: T_NetcdfTypes | None = None,
40114010
group: str | None = None,
40124011
engine: T_NetcdfEngine | None = None,

xarray/core/dataset.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
from xarray.core.missing import get_clean_interp_index
8989
from xarray.core.options import OPTIONS, _get_keep_attrs
9090
from xarray.core.types import (
91-
NetcdfWriteModes,
9291
QuantileMethods,
9392
Self,
9493
T_ChunkDim,
@@ -2172,7 +2171,7 @@ def dump_to_store(self, store: AbstractDataStore, **kwargs) -> None:
21722171
def to_netcdf(
21732172
self,
21742173
path: None = None,
2175-
mode: NetcdfWriteModes = "w",
2174+
mode: Literal["w", "a"] = "w",
21762175
format: T_NetcdfTypes | None = None,
21772176
group: str | None = None,
21782177
engine: T_NetcdfEngine | None = None,
@@ -2187,7 +2186,7 @@ def to_netcdf(
21872186
def to_netcdf(
21882187
self,
21892188
path: str | PathLike,
2190-
mode: NetcdfWriteModes = "w",
2189+
mode: Literal["w", "a"] = "w",
21912190
format: T_NetcdfTypes | None = None,
21922191
group: str | None = None,
21932192
engine: T_NetcdfEngine | None = None,
@@ -2203,7 +2202,7 @@ def to_netcdf(
22032202
def to_netcdf(
22042203
self,
22052204
path: str | PathLike,
2206-
mode: NetcdfWriteModes = "w",
2205+
mode: Literal["w", "a"] = "w",
22072206
format: T_NetcdfTypes | None = None,
22082207
group: str | None = None,
22092208
engine: T_NetcdfEngine | None = None,
@@ -2219,7 +2218,7 @@ def to_netcdf(
22192218
def to_netcdf(
22202219
self,
22212220
path: str | PathLike,
2222-
mode: NetcdfWriteModes = "w",
2221+
mode: Literal["w", "a"] = "w",
22232222
format: T_NetcdfTypes | None = None,
22242223
group: str | None = None,
22252224
engine: T_NetcdfEngine | None = None,
@@ -2232,7 +2231,7 @@ def to_netcdf(
22322231
def to_netcdf(
22332232
self,
22342233
path: str | PathLike | None = None,
2235-
mode: NetcdfWriteModes = "w",
2234+
mode: Literal["w", "a"] = "w",
22362235
format: T_NetcdfTypes | None = None,
22372236
group: str | None = None,
22382237
engine: T_NetcdfEngine | None = None,

xarray/core/datatree.py

+6-47
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
Any,
1010
Callable,
1111
Generic,
12-
Literal,
1312
NoReturn,
1413
Union,
1514
overload,
1615
)
1716

1817
from xarray.core import utils
19-
from xarray.core.common import TreeAttrAccessMixin
2018
from xarray.core.coordinates import DatasetCoordinates
2119
from xarray.core.dataarray import DataArray
2220
from xarray.core.dataset import Dataset, DataVariables
@@ -48,6 +46,7 @@
4846
maybe_wrap_array,
4947
)
5048
from xarray.core.variable import Variable
49+
from xarray.datatree_.datatree.common import TreeAttrAccessMixin
5150

5251
try:
5352
from xarray.core.variable import calculate_dimensions
@@ -58,9 +57,8 @@
5857
if TYPE_CHECKING:
5958
import pandas as pd
6059

61-
from xarray.core.datatree_io import T_DataTreeNetcdfEngine, T_DataTreeNetcdfTypes
6260
from xarray.core.merge import CoercibleValue
63-
from xarray.core.types import ErrorOptions, NetcdfWriteModes, ZarrWriteModes
61+
from xarray.core.types import ErrorOptions
6462

6563
# """
6664
# DEVELOPERS' NOTE
@@ -1477,16 +1475,7 @@ def groups(self):
14771475
return tuple(node.path for node in self.subtree)
14781476

14791477
def to_netcdf(
1480-
self,
1481-
filepath,
1482-
mode: NetcdfWriteModes = "w",
1483-
encoding=None,
1484-
unlimited_dims=None,
1485-
format: T_DataTreeNetcdfTypes | None = None,
1486-
engine: T_DataTreeNetcdfEngine | None = None,
1487-
group: str | None = None,
1488-
compute: bool = True,
1489-
**kwargs,
1478+
self, filepath, mode: str = "w", encoding=None, unlimited_dims=None, **kwargs
14901479
):
14911480
"""
14921481
Write datatree contents to a netCDF file.
@@ -1510,47 +1499,26 @@ def to_netcdf(
15101499
By default, no dimensions are treated as unlimited dimensions.
15111500
Note that unlimited_dims may also be set via
15121501
``dataset.encoding["unlimited_dims"]``.
1513-
format : {"NETCDF4", }, optional
1514-
File format for the resulting netCDF file:
1515-
1516-
* NETCDF4: Data is stored in an HDF5 file, using netCDF4 API features.
1517-
engine : {"netcdf4", "h5netcdf"}, optional
1518-
Engine to use when writing netCDF files. If not provided, the
1519-
default engine is chosen based on available dependencies, with a
1520-
preference for "netcdf4" if writing to a file on disk.
1521-
group : str, optional
1522-
Path to the netCDF4 group in the given file to open as the root group
1523-
of the ``DataTree``. Currently, specifying a group is not supported.
1524-
compute : bool, default: True
1525-
If true compute immediately, otherwise return a
1526-
``dask.delayed.Delayed`` object that can be computed later.
1527-
Currently, ``compute=False`` is not supported.
15281502
kwargs :
15291503
Addional keyword arguments to be passed to ``xarray.Dataset.to_netcdf``
15301504
"""
1531-
from xarray.core.datatree_io import _datatree_to_netcdf
1505+
from xarray.datatree_.datatree.io import _datatree_to_netcdf
15321506

15331507
_datatree_to_netcdf(
15341508
self,
15351509
filepath,
15361510
mode=mode,
15371511
encoding=encoding,
15381512
unlimited_dims=unlimited_dims,
1539-
format=format,
1540-
engine=engine,
1541-
group=group,
1542-
compute=compute,
15431513
**kwargs,
15441514
)
15451515

15461516
def to_zarr(
15471517
self,
15481518
store,
1549-
mode: ZarrWriteModes = "w-",
1519+
mode: str = "w-",
15501520
encoding=None,
15511521
consolidated: bool = True,
1552-
group: str | None = None,
1553-
compute: Literal[True] = True,
15541522
**kwargs,
15551523
):
15561524
"""
@@ -1573,26 +1541,17 @@ def to_zarr(
15731541
consolidated : bool
15741542
If True, apply zarr's `consolidate_metadata` function to the store
15751543
after writing metadata for all groups.
1576-
group : str, optional
1577-
Group path. (a.k.a. `path` in zarr terminology.)
1578-
compute : bool, default: True
1579-
If true compute immediately, otherwise return a
1580-
``dask.delayed.Delayed`` object that can be computed later. Metadata
1581-
is always updated eagerly. Currently, ``compute=False`` is not
1582-
supported.
15831544
kwargs :
15841545
Additional keyword arguments to be passed to ``xarray.Dataset.to_zarr``
15851546
"""
1586-
from xarray.core.datatree_io import _datatree_to_zarr
1547+
from xarray.datatree_.datatree.io import _datatree_to_zarr
15871548

15881549
_datatree_to_zarr(
15891550
self,
15901551
store,
15911552
mode=mode,
15921553
encoding=encoding,
15931554
consolidated=consolidated,
1594-
group=group,
1595-
compute=compute,
15961555
**kwargs,
15971556
)
15981557

xarray/core/types.py

-1
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,4 @@ def copy(
281281
]
282282

283283

284-
NetcdfWriteModes = Literal["w", "a"]
285284
ZarrWriteModes = Literal["w", "w-", "a", "a-", "r+", "r"]

0 commit comments

Comments
 (0)