Skip to content

Conversation

@FBumann
Copy link

@FBumann FBumann commented Jan 9, 2026

Summary

Add typed properties for external accessor packages (hvplot, cf-xarray, pint-xarray, rioxarray, xarray-plotly), enabling full IDE support including autocompletion, parameter hints, docstrings, and go-to-definition.

Problem

External accessor packages currently 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.

Solution

Add typed properties with lazy imports on DataArray, Dataset, and DataTree:

# In xarray/core/dataarray.py
if TYPE_CHECKING:
    from hvplot.xarray import hvPlotAccessor

class DataArray:
    @property
    def hvplot(self) -> hvPlotAccessor:
        """hvPlot accessor for interactive plotting."""
        from xarray.accessors import DATAARRAY_ACCESSORS, _get_external_accessor
        return _get_external_accessor("hvplot", self, DATAARRAY_ACCESSORS)

Key behaviors

  1. hasattr() returns False for uninstalled packages - Clean runtime introspection

    hasattr(da, 'hvplot')  # False (if hvplot not installed)
    hasattr(da, 'plotly')  # True (if xarray-plotly installed)
  2. Helpful error messages when accessing uninstalled accessors:

    da.hvplot
    # AttributeError: 'DataArray' object has no attribute 'hvplot'.
    # Install with: pip install hvplot
  3. External packages don't overwrite the typed properties - xarray's register_*_accessor skips registration for known external accessors, preserving IDE support.

  4. Full IDE support for installed packages - autocompletion, parameter hints, docstrings all work.

Supported accessors

Accessor Package DataArray Dataset DataTree
hvplot hvplot
cf cf-xarray
pint pint-xarray
rio rioxarray
plotly xarray-plotly

Files changed

  • xarray/accessors.py (new) - Accessor registry and lazy loader
  • xarray/core/dataarray.py - Added typed accessor properties
  • xarray/core/dataset.py - Added typed accessor properties
  • xarray/core/datatree.py - Added typed accessor properties
  • xarray/core/extensions.py - Skip registration for known external accessors
  • pyproject.toml - Added mypy ignores for external packages

Backward compatibility

  • Fully backward compatible - Existing code continues to work unchanged
  • External packages using register_*_accessor continue to work (registration is silently skipped for known accessors)
  • Users who don't use external accessors see no difference

Example usage

import xarray as xr

da = xr.DataArray([1, 2, 3], dims=['x'])

# IDE now provides full autocompletion for installed packages!
da.hvplot.line()  # If hvplot installed: works with full IDE support
da.cf.describe()  # If cf-xarray installed: works with full IDE support

# Helpful errors for uninstalled packages
da.rio  # AttributeError with install instructions

Related

@welcome
Copy link

welcome bot commented Jan 9, 2026

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

@github-actions github-actions bot added the topic-DataTree Related to the implementation of a DataTree class label Jan 9, 2026
@jsignell jsignell added API design and removed topic-DataTree Related to the implementation of a DataTree class labels Jan 9, 2026
@github-actions github-actions bot added the topic-DataTree Related to the implementation of a DataTree class label Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API design topic-DataTree Related to the implementation of a DataTree class

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve IDE/Editor Support for External Accessors

2 participants