Skip to content

Static typing for popular accessors#11087

Open
jsignell wants to merge 13 commits intopydata:mainfrom
jsignell:feature/accessor-improvement
Open

Static typing for popular accessors#11087
jsignell wants to merge 13 commits intopydata:mainfrom
jsignell:feature/accessor-improvement

Conversation

@jsignell
Copy link
Member

Based off of #11079 but only does the typing part.

Example of static typing:

image

Example of dynamic typing (in ipython) for the case where there is a data variable named one of the accessor names:

image

FBumann and others added 11 commits January 9, 2026 20:29
The pixi workspace includes pyproject.toml as source metadata for the
local xarray package (via `xarray = { path = "." }`). When pyproject.toml
changes, the cached pixi.lock becomes invalid, causing CI failures with
"lock-file not up-to-date with the workspace".

This fix adds pyproject.toml to the cache key hash, ensuring the lock
file is regenerated when either pixi.toml or pyproject.toml changes.
@github-actions github-actions bot added the topic-DataTree Related to the implementation of a DataTree class label Jan 14, 2026
Comment on lines +15 to +20
if TYPE_CHECKING:
from cf_xarray.accessor import CFAccessor
from hvplot.xarray import hvPlotAccessor
from pint_xarray import PintDataArrayAccessor, PintDatasetAccessor
from rioxarray import RasterArray, RasterDataset
from xarray_plotly import DataArrayPlotlyAccessor, DatasetPlotlyAccessor
Copy link
Member

@TomNicholas TomNicholas Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do all of these unrelated packages now have to be installed in our CI (in the same environment too)? Plotly at least is new.

Copy link
Member Author

@jsignell jsignell Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the mypy environment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually they don't need to be installed at all since they are only imported within the TYPE_CHECKING block. So we just need some explicit mypy ignores (which @FBumann had already figured out).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsignell Pretty elegant solution with the MixIn. I like it!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the IDE completion/hints still work even if not all of these are installed?

Copy link

@FBumann FBumann Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DocOtak Yes they do. But they are deprioritized (at least in my IDE), saying they are not at the top of the list

Here with only xarray-plotly installed, I only see the plotly accessor.

image image

But after I type in .hv, also see that accessor with type hints.

Completion after the accessor name is only available for installed accessors:

image image

One more thing:
The data_vars overwrite the accessor. This is a good thing imo!
image

The existing .plot accessor behaves differently. It overwrites the data_var
image

This means with conflicting var_names and accessor names, the code behaviour is backwards compatible. Only conflict in type hints, not at Runtime.

Copy link
Collaborator

@headtr1ck headtr1ck Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this "break" mypy for people only using xarray and only having installed a few or none accessors?
They might have their own pyproject.toml without these ignores.

The pixi workspace includes pyproject.toml as source metadata for the
local xarray package (via `xarray = { path = "." }`). When pyproject.toml
changes, the cached pixi.lock becomes invalid, causing CI failures with
"lock-file not up-to-date with the workspace".

This fix adds pyproject.toml to the cache key hash, ensuring the lock
file is regenerated when either pixi.toml or pyproject.toml changes.
@github-actions github-actions bot added the Automation Github bots, testing workflows, release automation label Jan 14, 2026
Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking these accessors are not ClassVar[type[XXAccessor]] but wrapped in an _CachedAccessor which is currently not typed at all.

Basically you can use it in two ways:
xr.Dataset.rio returns the class of the accessor and xr.Dataset().rio returns an instance of the accessor.

You can copy over the typing from the UncachedAccessor.

pint-xarray, rioxarray, xarray-plotly), enabling full IDE support including
autocompletion, parameter hints, and docstrings. For uninstalled packages,
``hasattr()`` returns ``False`` to keep the namespace clean (:pull:`11079`).
By `Your Name <https://github.com/FBumann>`_.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to fix "Your Name"?

@headtr1ck
Copy link
Collaborator

I'm all for this change, it just makes the live of users much easier.

We just have to decide how to handle requests from small/niche accessor developers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Automation Github bots, testing workflows, release automation needs discussion 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

5 participants