Skip to content

Commit 243c95c

Browse files
committed
Group decoding options into single argument
1 parent 8796d55 commit 243c95c

18 files changed

+769
-754
lines changed

doc/api-hidden.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@
680680
backends.BackendArray
681681
backends.BackendEntrypoint.guess_can_open
682682
backends.BackendEntrypoint.open_dataset
683+
backends.CoderOptions
684+
backends.CoderOptions.to_kwargs
683685

684686
core.indexing.IndexingSupport
685687
core.indexing.explicit_indexing_adapter

doc/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ Advanced API
16851685
Dataset.set_close
16861686
backends.BackendArray
16871687
backends.BackendEntrypoint
1688+
backends.CoderOptions
16881689
backends.list_engines
16891690
backends.refresh_engines
16901691

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,23 @@ This is what a ``BackendEntrypoint`` subclass should look like:
3838

3939
.. code-block:: python
4040
41-
from xarray.backends import BackendEntrypoint
41+
from xarray.backends import BackendEntrypoint, CoderOptions
4242
4343
4444
class MyBackendEntrypoint(BackendEntrypoint):
45+
coder_class = CoderOptions
46+
4547
def open_dataset(
4648
self,
4749
filename_or_obj,
4850
*,
49-
drop_variables=None,
51+
coder_options=None,
5052
# other backend specific keyword arguments
5153
# `chunks` and `cache` DO NOT go here, they are handled by xarray
5254
):
53-
return my_open_dataset(filename_or_obj, drop_variables=drop_variables)
55+
return my_open_dataset(filename_or_obj, coder_options=coder_options)
5456
55-
open_dataset_parameters = ["filename_or_obj", "drop_variables"]
57+
open_dataset_parameters = ["filename_or_obj", "coder_options"]
5658
5759
def guess_can_open(self, filename_or_obj):
5860
try:
@@ -83,19 +85,15 @@ The following is an example of the high level processing steps:
8385
self,
8486
filename_or_obj,
8587
*,
86-
drop_variables=None,
87-
decode_times=True,
88-
decode_timedelta=True,
89-
decode_coords=True,
88+
coder_options=None,
9089
my_backend_option=None,
9190
):
9291
vars, attrs, coords = my_reader(
9392
filename_or_obj,
94-
drop_variables=drop_variables,
9593
my_backend_option=my_backend_option,
9694
)
9795
vars, attrs, coords = my_decode_variables(
98-
vars, attrs, decode_times, decode_timedelta, decode_coords
96+
vars, attrs, **coder_options.to_kwargs()
9997
) # see also conventions.decode_cf_variables
10098
10199
ds = xr.Dataset(vars, attrs=attrs, coords=coords)
@@ -110,29 +108,26 @@ method shall be set by using :py:meth:`~xarray.Dataset.set_close`.
110108

111109

112110
The input of ``open_dataset`` method are one argument
113-
(``filename_or_obj``) and one keyword argument (``drop_variables``):
111+
(``filename_or_obj``) and one keyword argument (``coder_options``):
114112

115113
- ``filename_or_obj``: can be any object but usually it is a string containing a path or an instance of
116114
:py:class:`pathlib.Path`.
117-
- ``drop_variables``: can be ``None`` or an iterable containing the variable
118-
names to be dropped when reading the data.
115+
- ``coder_options``: can be None or :py:class:`~xarray.backends.CoderOptions`
119116

120-
If it makes sense for your backend, your ``open_dataset`` method
121-
should implement in its interface the following boolean keyword arguments, called
122-
**decoders**, which default to ``None``:
117+
If it makes sense for your backend, you can override the ``CoderOptions`` fields, which default to ``None``:
123118

124119
- ``mask_and_scale``
125120
- ``decode_times``
126121
- ``decode_timedelta``
127122
- ``use_cftime``
128123
- ``concat_characters``
129124
- ``decode_coords``
125+
- ``drop_variables``
130126

131-
Note: all the supported decoders shall be declared explicitly
132-
in backend ``open_dataset`` signature and adding a ``**kwargs`` is not allowed.
127+
Note: If ``coder_options`` is None the given kwargs are validated against the default.
133128

134129
These keyword arguments are explicitly defined in Xarray
135-
:py:func:`~xarray.open_dataset` signature. Xarray will pass them to the
130+
:py:func:`~xarray.CoderOptions` or subclass. Xarray will pass them to the
136131
backend only if the User explicitly sets a value different from ``None``.
137132
For more details on decoders see :ref:`RST decoders`.
138133

@@ -141,7 +136,6 @@ arguments. All these keyword arguments can be passed to
141136
:py:func:`~xarray.open_dataset` grouped either via the ``backend_kwargs``
142137
parameter or explicitly using the syntax ``**kwargs``.
143138

144-
145139
If you don't want to support the lazy loading, then the
146140
:py:class:`~xarray.Dataset` shall contain values as a :py:class:`numpy.ndarray`
147141
and your work is almost done.
@@ -260,14 +254,16 @@ time is stored in two attributes dataDate and dataTime as strings. Therefore,
260254
it is not possible to reuse the Xarray time decoder, and implementing a new
261255
one is mandatory.
262256

263-
Decoders can be activated or deactivated using the boolean keywords of
264-
Xarray :py:meth:`~xarray.open_dataset` signature: ``mask_and_scale``,
257+
Decoders can be activated or deactivated using ``coder_options`` kwarg
258+
(:py:class:`~xarray.backends.CoderOptions`) or it's boolean keywords equivalent of
259+
Xarray :py:meth:`~xarray.open_dataset` (``mask_and_scale``,
265260
``decode_times``, ``decode_timedelta``, ``use_cftime``,
266-
``concat_characters``, ``decode_coords``.
261+
``concat_characters``, ``decode_coords``. ``drop_variables``)
267262
Such keywords are passed to the backend only if the User sets a value
268263
different from ``None``. Note that the backend does not necessarily have to
269-
implement all the decoders, but it shall declare in its ``open_dataset``
270-
interface only the boolean keywords related to the supported decoders.
264+
implement all the decoders, but it shall declare a ``coder_class`` in its
265+
``BackendEntrypoint`` interface with only the boolean keywords related to
266+
the supported decoders.
271267

272268
.. _RST backend_registration:
273269

xarray/backends/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
formats. They should not be used directly, but rather through Dataset objects.
55
"""
66

7-
from xarray.backends.common import AbstractDataStore, BackendArray, BackendEntrypoint
7+
from xarray.backends.common import (
8+
AbstractDataStore,
9+
BackendArray,
10+
BackendEntrypoint,
11+
CoderOptions,
12+
)
813
from xarray.backends.file_manager import (
914
CachingFileManager,
1015
DummyFileManager,
@@ -24,6 +29,7 @@
2429
"BackendArray",
2530
"BackendEntrypoint",
2631
"CachingFileManager",
32+
"CoderOptions",
2733
"DummyFileManager",
2834
"FileManager",
2935
"H5NetCDFStore",

0 commit comments

Comments
 (0)