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

Conversation

ZedThree
Copy link
Contributor

See #8288

  • Closes #xxxx
  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst
  • New functions/methods are listed in api.rst

These changes allow me to make a pretty minimal subclass that just swaps out the netCDF4.Dataset class for my version:

from xarray.backends.netCDF4_ import NetCDF4BackendEntrypoint, NetCDF4DataStore
from ._wrapper import Dataset  # This is my netCDF extension


class NcComplexDataStore(NetCDF4DataStore):
    @classmethod
    def DatasetClass(self) -> type:
        return Dataset


class NcComplexBackendEntrypoint(NetCDF4BackendEntrypoint):
    DataStore = NcComplexDataStore

@ZedThree
Copy link
Contributor Author

Failing with lots of timeouts in the MacOS py3.11 build, and all in TestNetCDF4ViaDaskData, TestZarrDirectoryStore, TestZarrDictStore -- plausibly related to this change?

@kmuehlbauer
Copy link
Contributor

@ZedThree I've restarted CI, let's see if it was just flaky.

@ZedThree
Copy link
Contributor Author

Thanks! Looks like it was just flaky

Copy link
Collaborator

@mathause mathause left a comment

Choose a reason for hiding this comment

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

I am not super familiar with the backend stuff, but I left some comments that should make it a bit clearer.

Comment on lines +567 to +568
DataStore = NetCDF4DataStore
# This is to allow subclassing this backend entrypoint
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

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

return netCDF4.Dataset
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

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

This change feels a bit weird, and only to support easy subclassing.
I'm in favor of optimizing for simpler subclassing but not if it makes code less readable.

Also this breaks several type checks (which in this case doesn't matter because netCDF4 is still untyped).

Maybe instead you could factor out the parts that require netCDF4 such that you can easily override these in the subclass?

@ZedThree
Copy link
Contributor Author

ZedThree commented Dec 4, 2023

Sorry, this is no longer needed, as we're now going to add the functionality straight to the netCDF4 Python library. xarray will then need only need a new argument adding here, instead of needing a whole new backend

@ZedThree ZedThree closed this Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants