Skip to content

generate datatree methods #10146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Conversation

mathause
Copy link
Collaborator

@mathause mathause commented Mar 18, 2025

  • Closes DataTree: missing methods #10015
  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst
  • New functions/methods are listed in api.rst

Add a script that generates a mixin so Dataset methods are available on DataTree. Uses inspect.signature to re-generate the call signature and a decorator so we can still use *args, **kwargs, and we don't need to populate the method body, making the generation relatively trivial (although maybe not trivial to understand).

This is much clunkier than generate_ops or generate_aggregations. However, we cannot profit from common signatures. Thus

  • the docstring is not adapted
  • the examples are not adapted
  • the generated file needs to be fixed and formatted with ruff before use

However, it's a fraction of the work to do this properly. I am really not sure if this is a good idea - feel free to tell me it's not!

@mathause
Copy link
Collaborator Author

The alternative is to inject everything (as in https://github.com/xarray-contrib/datatree/blob/5f3956ffe80e686dd3df54ee8cef9ff56c158e76/datatree/ops.py#L223). (Or to write all methods out, or create mixin classes that work for all data types...)

@github-actions github-actions bot added topic-documentation topic-DataTree Related to the implementation of a DataTree class labels Jun 5, 2025
@mathause
Copy link
Collaborator Author

mathause commented Jun 5, 2025

@TomNicholas do you think this has a chance to be considered. If not I happy to close. It's obviously only a fraction of the missing methods - can add them if this is considered.

One alternative is to generate the file once and then manually adapt the docstrings. That would be a bit less work than do everything by hand. (It's quite annoying to always write xr.map_over_datasets(lambda ds: ds.rename(...), dt) instead of dt.rename(...), etc. for many of the dataset manipulations.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-DataTree Related to the implementation of a DataTree class topic-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataTree: missing methods
1 participant