Skip to content

Commit bfea52d

Browse files
committed
Consistently use BytesIOProxy
1 parent 3fd0de4 commit bfea52d

3 files changed

Lines changed: 9 additions & 17 deletions

File tree

xarray/backends/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ def to_netcdf(
21242124

21252125
if path_or_file is None:
21262126
assert isinstance(target, BytesIOProxy) # created in this function
2127-
return target.getvalue_or_getbuffer()
2127+
return target.getbuffer()
21282128

21292129
if not compute:
21302130
return delayed_close_after_writes(writes, store)

xarray/backends/common.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
TYPE_CHECKING,
1212
Any,
1313
ClassVar,
14-
Generic,
1514
Self,
1615
TypeVar,
1716
Union,
@@ -198,18 +197,13 @@ def _normalize_path_list(
198197
return _normalize_path_list(paths)
199198

200199

201-
BytesOrMemory = TypeVar("BytesOrMemory", bytes, memoryview)
202-
203-
204200
@dataclass
205-
class BytesIOProxy(Generic[BytesOrMemory]):
206-
"""Proxy object for a write that returns either bytes or a memoryview."""
201+
class BytesIOProxy:
202+
"""Proxy object for a write that a memoryview."""
207203

208-
# TODO: remove this in favor of BytesIO when Dataset.to_netcdf() stops
209-
# returning bytes from the scipy engine
210-
getvalue: Callable[[], BytesOrMemory] | None = None
204+
getvalue: Callable[[], memoryview] | None = None
211205

212-
def getvalue_or_getbuffer(self) -> BytesOrMemory:
206+
def getbuffer(self) -> memoryview:
213207
"""Get the value of this write as bytes or memory."""
214208
if self.getvalue is None:
215209
raise ValueError("must set getvalue before fetching value")

xarray/core/datatree_io.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
get_writable_netcdf_store,
1414
get_writable_zarr_store,
1515
)
16-
from xarray.backends.common import ArrayWriter
16+
from xarray.backends.common import ArrayWriter, BytesIOProxy
1717
from xarray.backends.locks import get_dask_scheduler
1818
from xarray.core.datatree import DataTree
1919
from xarray.core.types import NetcdfWriteModes, ZarrWriteModes
@@ -81,11 +81,9 @@ def _datatree_to_netcdf(
8181
if not compute:
8282
raise NotImplementedError(
8383
"to_netcdf() with compute=False is not yet implemented when "
84-
"returning bytes"
84+
"returning a memoryview"
8585
)
86-
# No need to use BytesIOProxy here because the legacy scipy backend
87-
# cannot write netCDF files with groups
88-
target = io.BytesIO()
86+
target = BytesIOProxy()
8987
else:
9088
target = filepath # type: ignore[assignment]
9189

@@ -139,7 +137,7 @@ def _datatree_to_netcdf(
139137
root_store.sync()
140138

141139
if filepath is None:
142-
assert isinstance(target, io.BytesIO)
140+
assert isinstance(target, BytesIOProxy) # created in this function
143141
return target.getbuffer()
144142

145143
if not compute:

0 commit comments

Comments
 (0)