pydata / pydata/xarray

Improve IDE/Editor Support for External Accessors

Open
#11,078 29 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement topic-typing
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?

External accessor packages have no IDE support:

import xarray as xr
import hvplot.xarray  # required just to register accessor

ds = xr.open_dataset("data.nc")

ds.plot.scatter()    # Built-in: full IDE completion ✓
ds.hvplot.scatter()  # External: nothing. no hints, no docs ✗
Feature Built-in .plot External Accessor
Method completion
Parameter hints
Docstrings
Go-to-definition

This affects every accessor package: hvplot, cf-xarray, pint-xarray, rioxarray, xarray-plotly, and the entire xarray-contrib ecosystem.

The current message to package authors is "use the accessor system" - but that system provides a degraded experience compared to built-in features.

Describe the solution you'd like

Typed properties with lazy imports. Same pattern as dask.distributed.

# xarray/core/dataarray.py

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from hvplot.xarray import hvPlotAccessor
    from cf_xarray import CFAccessor

class DataArray:

    @property
    def hvplot(self) -> hvPlotAccessor:
        """hvPlot accessor. Requires: pip install hvplot"""
        from xarray.accessors import load_accessor
        return load_accessor("hvplot", self)

    @property
    def cf(self) -> CFAccessor:
        """CF conventions accessor. Requires: pip install cf-xarray"""
        from xarray.accessors import load_accessor
        return load_accessor("cf", self)
# xarray/accessors.py

ACCESSORS = {
    "hvplot": ("hvplot.xarray", "hvPlotAccessor", "hvplot"),
    "cf": ("cf_xarray", "CFAccessor", "cf-xarray"),
    "pint": ("pint_xarray", "PintDataArrayAccessor", "pint-xarray"),
    "rio": ("rioxarray", "RasterArray", "rioxarray"),
    "plotly": ("xarray_plotly", "DataArrayPlotlyAccessor", "xarray-plotly"),
}

def load_accessor(name: str, obj):
    package, cls_name, install_name = ACCESSORS[name]
    try:
        module = importlib.import_module(package)
        return getattr(module, cls_name)(obj)
    except ImportError:
        raise ImportError(f"{install_name} is not installed.\n\nInstall with: pip install {install_name}")

User experience after:

import xarray as xr

ds.hvplot.scatter()  # just works, full IDE support, no registration import!

Benefits:

  • Users: No registration imports, full IDE completion, helpful errors if not installed
  • Accessor maintainers: File PR adding ~5 lines, remove register_*_accessor boilerplate
  • xarray: Stays lean, implementation remains external, fully backwards compatible
Describe alternatives you've considered

Status quo: Keep register_*_accessor - but IDE support is impossible since types are unknown at static analysis time.

Stub files (.pyi): External packages could ship stubs, but this doesn't solve the "xarray doesn't know about the accessor" problem - the property still needs to exist on DataArray/Dataset.

Additional context

Backwards compatible: Old packages using register_*_accessor continue to work (setattr overwrites property). New packages remove registration and users get IDE completion.

Precedent: This is exactly how dask handles dask.distributed - typed entry point in dask, implementation in separate package.

Origin: PR #11075 - accessor was rejected (correctly) in favor of external package, but this revealed the IDE support gap.

cc: @xarray-contrib @holoviz @corteva

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading xarray/core/dataarray.py and xarray/accessors.py, then review PR #11075 for the accessor design context. Assess the proposed typed properties, lazy loading, and compatibility with existing register_*_accessor packages. Done means external accessors have discoverable IDE types and useful missing-package errors without breaking current registration behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.