Skip to content

Commit 186c2ff

Browse files
authored
Simplify the user experience of a backend developer (imports and docs) (#5127)
* Simplify the user experience of a backend developer (imports and docs) * Code style * Use RST links
1 parent 32ccc93 commit 186c2ff

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

doc/internals/how-to-add-new-backend.rst

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ This is what a ``BackendEntrypoint`` subclass should look like:
3232

3333
.. code-block:: python
3434
35+
from xarray.backends import BackendEntrypoint
36+
37+
3538
class MyBackendEntrypoint(BackendEntrypoint):
3639
def open_dataset(
3740
self,
3841
filename_or_obj,
3942
*,
4043
drop_variables=None,
4144
# other backend specific keyword arguments
45+
# `chunks` and `cache` DO NOT go here, they are handled by xarray
4246
):
43-
...
44-
return ds
47+
return my_open_dataset(filename_or_obj, drop_variables=drop_variables)
4548
4649
open_dataset_parameters = ["filename_or_obj", "drop_variables"]
4750
@@ -50,7 +53,7 @@ This is what a ``BackendEntrypoint`` subclass should look like:
5053
_, ext = os.path.splitext(filename_or_obj)
5154
except TypeError:
5255
return False
53-
return ext in {...}
56+
return ext in {".my_format", ".my_fmt"}
5457
5558
``BackendEntrypoint`` subclass methods and attributes are detailed in the following.
5659

@@ -74,20 +77,19 @@ The following is an example of the high level processing steps:
7477
decode_times=True,
7578
decode_timedelta=True,
7679
decode_coords=True,
77-
my_backend_param=None,
80+
my_backend_option=None,
7881
):
7982
vars, attrs, coords = my_reader(
8083
filename_or_obj,
8184
drop_variables=drop_variables,
82-
my_backend_param=my_backend_param,
85+
my_backend_option=my_backend_option,
8386
)
8487
vars, attrs, coords = my_decode_variables(
8588
vars, attrs, decode_times, decode_timedelta, decode_coords
8689
) # see also conventions.decode_cf_variables
8790
88-
ds = xr.Dataset(vars, attrs=attrs)
89-
ds = ds.set_coords(coords)
90-
ds.set_close(store.close)
91+
ds = xr.Dataset(vars, attrs=attrs, coords=coords)
92+
ds.set_close(my_close_method)
9193
9294
return ds
9395
@@ -98,9 +100,9 @@ method shall be set by using :py:meth:`~xarray.Dataset.set_close`.
98100

99101

100102
The input of ``open_dataset`` method are one argument
101-
(``filename``) and one keyword argument (``drop_variables``):
103+
(``filename_or_obj``) and one keyword argument (``drop_variables``):
102104

103-
- ``filename``: can be a string containing a path or an instance of
105+
- ``filename_or_obj``: can be any object but usually it is a string containing a path or an instance of
104106
:py:class:`pathlib.Path`.
105107
- ``drop_variables``: can be `None` or an iterable containing the variable
106108
names to be dropped when reading the data.
@@ -117,7 +119,7 @@ should implement in its interface the following boolean keyword arguments, calle
117119
- ``decode_coords``
118120

119121
Note: all the supported decoders shall be declared explicitly
120-
in backend ``open_dataset`` signature.
122+
in backend ``open_dataset`` signature and adding a ``**kargs`` is not allowed.
121123

122124
These keyword arguments are explicitly defined in Xarray
123125
:py:func:`~xarray.open_dataset` signature. Xarray will pass them to the
@@ -241,7 +243,7 @@ How to register a backend
241243

242244
Define a new entrypoint in your ``setup.py`` (or ``setup.cfg``) with:
243245

244-
- group: ``xarray.backend``
246+
- group: ``xarray.backends``
245247
- name: the name to be passed to :py:meth:`~xarray.open_dataset` as ``engine``
246248
- object reference: the reference of the class that you have implemented.
247249

@@ -251,9 +253,7 @@ You can declare the entrypoint in ``setup.py`` using the following syntax:
251253
252254
setuptools.setup(
253255
entry_points={
254-
"xarray.backends": [
255-
"engine_name=your_package.your_module:YourBackendEntryClass"
256-
],
256+
"xarray.backends": ["my_engine=my_package.my_module:MyBackendEntryClass"],
257257
},
258258
)
259259
@@ -263,18 +263,18 @@ in ``setup.cfg``:
263263
264264
[options.entry_points]
265265
xarray.backends =
266-
engine_name = your_package.your_module:YourBackendEntryClass
266+
my_engine = my_package.my_module:MyBackendEntryClass
267267
268268
269269
See https://packaging.python.org/specifications/entry-points/#data-model
270270
for more information
271271

272-
If you are using [Poetry](https://python-poetry.org/) for your build system, you can accomplish the same thing using "plugins". In this case you would need to add the following to your ``pyproject.toml`` file:
272+
If you are using `Poetry <https://python-poetry.org/>`_ for your build system, you can accomplish the same thing using "plugins". In this case you would need to add the following to your ``pyproject.toml`` file:
273273

274274
.. code-block:: toml
275275
276276
[tool.poetry.plugins."xarray_backends"]
277-
"engine_name" = "your_package.your_module:YourBackendEntryClass"
277+
"my_engine" = "my_package.my_module:MyBackendEntryClass"
278278
279279
See https://python-poetry.org/docs/pyproject/#plugins for more information on Poetry plugins.
280280

@@ -328,6 +328,9 @@ This is an example ``BackendArray`` subclass implementation:
328328

329329
.. code-block:: python
330330
331+
from xarray.backends import BackendArray
332+
333+
331334
class MyBackendArray(BackendArray):
332335
def __init__(
333336
self,

xarray/backends/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
formats. They should not be used directly, but rather through Dataset objects.
55
"""
66
from .cfgrib_ import CfGribDataStore
7-
from .common import AbstractDataStore
7+
from .common import AbstractDataStore, BackendArray, BackendEntrypoint
88
from .file_manager import CachingFileManager, DummyFileManager, FileManager
99
from .h5netcdf_ import H5NetCDFStore
1010
from .memory import InMemoryDataStore
@@ -18,6 +18,8 @@
1818

1919
__all__ = [
2020
"AbstractDataStore",
21+
"BackendArray",
22+
"BackendEntrypoint",
2123
"FileManager",
2224
"CachingFileManager",
2325
"CfGribDataStore",

0 commit comments

Comments
 (0)