Skip to content
Draft
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
37 changes: 20 additions & 17 deletions xarray/backends/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import time
import traceback
from abc import ABC, abstractmethod
from collections.abc import Iterable
from glob import glob
from typing import TYPE_CHECKING, Any, ClassVar
Expand Down Expand Up @@ -170,17 +171,20 @@ def get_duck_array(self, dtype: np.typing.DTypeLike = None):
return self[key] # type: ignore [index]


class AbstractDataStore:
class AbstractDataStore(ABC):
__slots__ = ()

def get_dimensions(self): # pragma: no cover
raise NotImplementedError()
@abstractmethod
def get_dimensions(self):
...

def get_attrs(self): # pragma: no cover
raise NotImplementedError()
@abstractmethod
def get_attrs(self):
...

def get_variables(self): # pragma: no cover
raise NotImplementedError()
@abstractmethod
def get_variables(self):
...

def get_encoding(self):
return {}
Expand Down Expand Up @@ -300,14 +304,13 @@ def encode_attribute(self, a):
"""encode one attribute"""
return a

def set_dimension(self, dim, length): # pragma: no cover
raise NotImplementedError()
@abstractmethod
def set_dimension(self, dim, length):
...

def set_attribute(self, k, v): # pragma: no cover
raise NotImplementedError()

def set_variable(self, k, v): # pragma: no cover
raise NotImplementedError()
@abstractmethod
def set_attribute(self, k, v):
...

def store_dataset(self, dataset):
"""
Expand Down Expand Up @@ -444,7 +447,7 @@ def encode(self, variables, attributes):
return variables, attributes


class BackendEntrypoint:
class BackendEntrypoint(ABC):
"""
``BackendEntrypoint`` is a class container and it is the main interface
for the backend plugins, see :ref:`RST backend_entrypoint`.
Expand Down Expand Up @@ -485,6 +488,7 @@ def __repr__(self) -> str:
txt += f"\n Learn more at {self.url}"
return txt

@abstractmethod
Copy link
Member

Choose a reason for hiding this comment

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

Per #7437 (comment), we may not want to require all of these methods. We may end up in a situation where BackendEntrypoints must define one or more of open_dataarray, open_dataset, and open_datatree.

cc @jthielen, @keewis

Copy link
Member

Choose a reason for hiding this comment

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

This seems pretty doable, e.g.

class MyBaseClass:
    def __init__(self):
        super().__init__()

        # Check that at least one of the methods has been overridden
        if all(getattr(self.__class__, m) is getattr(MyBaseClass, m) for m in ['method1', 'method2', 'method3']):
            raise TypeError("You must implement at least one of 'method1', 'method2', 'method3'")

    def method1(self):
        pass

    def method2(self):
        pass

    def method3(self):
        pass

class MyDerivedClass(MyBaseClass):
    def method1(self):
        print("Method1 implemented")

# This will not raise an error
d = MyDerivedClass()

class MyInvalidDerivedClass(MyBaseClass):
    pass

# This will raise a TypeError
i = MyInvalidDerivedClass()
TypeError: You must implement at least one of 'method1', 'method2', 'method3'

def open_dataset(
self,
filename_or_obj: str | os.PathLike[Any] | BufferedIOBase | AbstractDataStore,
Expand All @@ -495,8 +499,7 @@ def open_dataset(
"""
Backend open_dataset method used by Xarray in :py:func:`~xarray.open_dataset`.
"""

raise NotImplementedError
...

def guess_can_open(
self,
Expand Down