Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable subclassing the netCDF4 backend, changing the dataset class #8338

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions xarray/backends/netCDF4_.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,11 @@ class NetCDF4DataStore(WritableCFDataStore):
def __init__(
self, manager, group=None, mode=None, lock=NETCDF4_PYTHON_LOCK, autoclose=False
):
import netCDF4

if isinstance(manager, netCDF4.Dataset):
if isinstance(manager, self.DatasetClass()):
if group is None:
root, group = find_root_and_group(manager)
else:
if type(manager) is not netCDF4.Dataset:
if type(manager) is not self.DatasetClass():
raise ValueError(
"must supply a root netCDF4.Dataset if the group "
"argument is provided"
Expand All @@ -350,6 +348,13 @@ def __init__(
self.lock = ensure_lock(lock)
self.autoclose = autoclose

@classmethod
def DatasetClass(self) -> type:
# This is to allow subclassing the datastore and retain lazy-importing
import netCDF4

return netCDF4.Dataset
Comment on lines +351 to +356
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


@classmethod
def open(
cls,
Expand All @@ -364,8 +369,6 @@ def open(
lock_maker=None,
autoclose=False,
):
import netCDF4

if isinstance(filename, os.PathLike):
filename = os.fspath(filename)

Expand Down Expand Up @@ -395,7 +398,7 @@ def open(
clobber=clobber, diskless=diskless, persist=persist, format=format
)
manager = CachingFileManager(
netCDF4.Dataset, filename, mode=mode, kwargs=kwargs
cls.DatasetClass(), filename, mode=mode, kwargs=kwargs
)
return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose)

Expand Down Expand Up @@ -561,6 +564,8 @@ class NetCDF4BackendEntrypoint(BackendEntrypoint):
"Open netCDF (.nc, .nc4 and .cdf) and most HDF5 files using netCDF4 in Xarray"
)
url = "https://docs.xarray.dev/en/stable/generated/xarray.backends.NetCDF4BackendEntrypoint.html"
DataStore = NetCDF4DataStore
# This is to allow subclassing this backend entrypoint
Comment on lines +567 to +568
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DataStore = NetCDF4DataStore
# This is to allow subclassing this backend entrypoint
# allows to subclass this backend entrypoint
DataStore = NetCDF4DataStore

This is a nit but we usually comment above the code


def guess_can_open(
self,
Expand Down Expand Up @@ -600,7 +605,7 @@ def open_dataset( # type: ignore[override] # allow LSP violation, not supporti
autoclose=False,
) -> Dataset:
filename_or_obj = _normalize_path(filename_or_obj)
store = NetCDF4DataStore.open(
store = self.DataStore.open(
filename_or_obj,
mode=mode,
format=format,
Expand Down
Loading