Open scott-huberty opened 1 month ago
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!
Yes this sounds like a really nice contribution!
I'm totally supporting this idea.
Just to get this into the discussion: in one of the packages I maintain this was extended to allow such scheme for global imports:
Found this while researching other options
I think that lazy loading and optional dependency handling are separate issues, at least as far as how it is implemented in lazy_loader
. For example with lazy_loader
I think that top level imports of optional dependencies will still raise an error (if that package is not installed).
For what it's worth MNE-Python is trying out lazy_loader
, and my current understanding is that the development team has not reached consensus on it ( see https://github.com/mne-tools/mne-python/pull/12059 )
This ticket proposes two separate but overlapping ideas.
1. When trying to write/read Zarr files, throw an informative error if the user does not have
zarr
installedjust as Xarray does for other optional dependencies like Matplotlib.
Reproducible Code
Assuming you only have the base Xarray deps installed: ```python import xarray as xr da = xr.DataArray([1, 2, 3], dims='x', coords={'x': [10, 20, 30]} ) da.plot() # Raises a more informative ImportError da.to_zarr('./test.zarr') # Does not raise an informative ImportError ```2. Create a utility function to handle optional dependency imports across the Xarray code base.
Doing
git grep "raise ImportError"
orgit grep "raise ModuleNotFoundError"
reveals that currently there is a piecemeal approach to handling optional dependencies. It would probably be helpful to have a dedicated function to handle soft dependencies across the code base, and I would be happy to add this If I am going to be adding a PR for point 1 anyways.Describe the solution you'd like
I work on an applied neuroscience package named MNE-Python, where we have a lot of optional dependencies that are only needed for specific MNE functionalities. So borrowing from MNE, I think something like this would be nice:
Then, for example, here, we can instead do:
And if we don't have
zarr
installed we'd get:Describe alternatives you've considered
Open to any ideas 🙂
Additional context
What do folks think about this idea? Is a PR welcome?