Skip to content

Add open_dataarray to BackendEntrypoint? #10562

Open

Description

@weiji14

Is your feature request related to a problem?

The BackendEntrypoint class at

class BackendEntrypoint:
"""
``BackendEntrypoint`` is a class container and it is the main interface
for the backend plugins, see :ref:`RST backend_entrypoint`.
It shall implement:
- ``open_dataset`` method: it shall implement reading from file, variables
decoding and it returns an instance of :py:class:`~xarray.Dataset`.
It shall take in input at least ``filename_or_obj`` argument and
``drop_variables`` keyword argument.
For more details see :ref:`RST open_dataset`.
- ``guess_can_open`` method: it shall return ``True`` if the backend is able to open
``filename_or_obj``, ``False`` otherwise. The implementation of this
method is not mandatory.
- ``open_datatree`` method: it shall implement reading from file, variables
decoding and it returns an instance of :py:class:`~datatree.DataTree`.
It shall take in input at least ``filename_or_obj`` argument. The
implementation of this method is not mandatory. For more details see
<reference to open_datatree documentation>.
Attributes
----------
open_dataset_parameters : tuple, default: None
A list of ``open_dataset`` method parameters.
The setting of this attribute is not mandatory.
description : str, default: ""
A short string describing the engine.
The setting of this attribute is not mandatory.
url : str, default: ""
A string with the URL to the backend's documentation.
The setting of this attribute is not mandatory.
"""
open_dataset_parameters: ClassVar[tuple | None] = None
description: ClassVar[str] = ""
url: ClassVar[str] = ""
def __repr__(self) -> str:
txt = f"<{type(self).__name__}>"
if self.description:
txt += f"\n {self.description}"
if self.url:
txt += f"\n Learn more at {self.url}"
return txt
def open_dataset(
self,
filename_or_obj: str | os.PathLike[Any] | ReadBuffer | AbstractDataStore,
*,
drop_variables: str | Iterable[str] | None = None,
) -> Dataset:
"""
Backend open_dataset method used by Xarray in :py:func:`~xarray.open_dataset`.
"""
raise NotImplementedError()
def guess_can_open(
self,
filename_or_obj: str | os.PathLike[Any] | ReadBuffer | AbstractDataStore,
) -> bool:
"""
Backend open_dataset method used by Xarray in :py:func:`~xarray.open_dataset`.
"""
return False
def open_datatree(
self,
filename_or_obj: str | os.PathLike[Any] | ReadBuffer | AbstractDataStore,
*,
drop_variables: str | Iterable[str] | None = None,
) -> DataTree:
"""
Backend open_datatree method used by Xarray in :py:func:`~xarray.open_datatree`.
"""
raise NotImplementedError()
currently has open_dataset and open_datatree methods, but not open_dataarray. Currently, someone implementing a BackendEntrypoint to do xarray.open_dataarray(..., engine="<something>") would have to implement open_dataset, and behind the scenes, this could result in a roundtrip xr.DataArray -> xr.Dataset -> xr.DataArray conversion, which can lead to some attributes/state getting lost along the way (xref #3268).

Describe the solution you'd like

Enable open_dataarray to be implemented directly, without a roundtrip conversion to/from xr.Dataset

Describe alternatives you've considered

No response

Additional context

GenericMappingTools/pygmt#4008 (comment)

Activity

  1. dncnmcdougall commented on Aug 19, 2026

    @dncnmcdougall

    I have just run face first into this.
    I am loading parts of a hdf5 file.
    What makes this challenging is that the logic of the arguments is different depending on whether I am loading a Dataarray, Dataset or DataTree.

    In addition, looking at the code, I see that the Backend.open_dataset_parameters is used for both opne_dataset and open_datatree.
    It would not be difficult to split this into three options.

    The only problem I can foresee is backwards comparability: if a new Backend.open_dataarray is introduced, old code using open_dataarray(...engine='thing') is secretly using Backend.open_dataset. So if the open_dataset and open_dataarray methods are different this code will break unexpectedly.

    But this can be overcome with warnings in client libraries, and giving open_dataarray a default implementation that throws NotImplementedError and then falling back to open_dataset or something.
    Likewise for Backend.open_datatree_parameters

    Are there any other design considerations?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions