Improve discoverability of index build options
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
Is your feature request related to a problem?
Currently Dataset.set_xindex(coord_names, index_cls=None, **options) allows passing index build options (if any) via the **options arguments. Those options are not easily discoverable, though (no auto-completion, etc.).
Describe the solution you'd like
What about something like this?
ds.set_xindex("x", MyCustomIndex.with_options(foo=1, bar=True))
# or
ds.set_xindex("x", *MyCustomIndex.with_options(foo=1, bar=True))
This would require adding a .with_options() class method that can be overridden in Index subclasses (optional):
# xarray.core.indexes
class Index:
@classmethod
def with_options(cls) -> tuple[type[Self], dict[str, Any]]:
return cls, {}
# third-party code
from xarray.indexes import Index
class MyCustomIndex(Index):
@classmethod
def with_options(cls, foo: int = 0, bar: bool = False) -> tuple[type[Self], dict[str, Any]]:
"""Set a new MyCustomIndex with options.
Parameters
------------
foo : int, optional
The foo option (default: 1).
bar : bool, optional
The bar option (default: False).
"""
return cls, {"foo": foo, "bar": bar}
Thoughts?
Describe alternatives you've considered
Build options are also likely defined in the Index constructor, e.g.,
# third-party code
from xarray.indexes import Index
class MyCustomIndex(Index):
def __init__(self, data, foo=0, bar=False):
...
However, the Index constructor is not public API (only used internally and indirectly in Xarray when setting a new index from existing coordinates).
Any other idea?
Additional context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Dataset.set_xindex and the Index class in xarray.core.indexes, then review how index build options are currently passed through **options and constructors. Compare the proposed with_options() forms and determine the public API and override behavior that should be adopted. Done means the option-discovery approach and its expected behavior are agreed and documented for implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100