-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Static typing for popular accessors #11087
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
Open
jsignell
wants to merge
13
commits into
pydata:main
Choose a base branch
from
jsignell:feature/accessor-improvement
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−1
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aa4c5b1
Add common accessors
FBumann 05aeef0
Add other error
FBumann 22314ea
Handle overwrites of known accessors (old version of accessor, new xa…
FBumann 3eacdd5
Add missing file , tests and notes
FBumann 477f1be
Move accessors to other module
FBumann e836ec6
Shorten docstrings
FBumann bb222e6
Merge branch 'main' into feature/accessor-improvement
FBumann 4c83113
Merge remote-tracking branch 'origin/main' into feature/accessor-impr…
FBumann 4c73974
fix(ci): include pyproject.toml in pixi lock cache key
FBumann 40a2f2c
Fix whats-new.rst
FBumann ce31134
Only put the types
jsignell 618c1c5
fix(ci): include pyproject.toml in pixi lock cache key
FBumann 1fed2ce
Add pyproject.toml skips
jsignell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ jobs: | |
| with: | ||
| path: | | ||
| pixi.lock | ||
| key: ${{ steps.date.outputs.date }}_${{ inputs.pixi-version }}_${{hashFiles('pixi.toml')}} | ||
| key: ${{ steps.date.outputs.date }}_${{ inputs.pixi-version }}_${{hashFiles('pixi.toml', 'pyproject.toml')}} | ||
| - uses: prefix-dev/[email protected] | ||
| if: ${{ !steps.restore.outputs.cache-hit }} | ||
| with: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """ | ||
| External accessor support for xarray. | ||
|
|
||
| This module provides mixin classes with typed properties for external accessor | ||
| packages, enabling full IDE support (autocompletion, parameter hints, docstrings) | ||
| for packages like hvplot, cf-xarray, pint-xarray, rioxarray, and xarray-plotly. | ||
|
|
||
| Properties are defined statically for IDE support | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, ClassVar | ||
|
|
||
| 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 | ||
|
|
||
|
|
||
| class DataArrayExternalAccessorMixin: | ||
| """Mixin providing typed external accessor properties for DataArray.""" | ||
|
|
||
| __slots__ = () | ||
|
|
||
| hvplot: ClassVar[type[hvPlotAccessor]] | ||
| cf: ClassVar[type[CFAccessor]] | ||
| pint: ClassVar[type[PintDataArrayAccessor]] | ||
| rio: ClassVar[type[RasterArray]] | ||
| plotly: ClassVar[type[DataArrayPlotlyAccessor]] | ||
|
|
||
|
|
||
| class DatasetExternalAccessorMixin: | ||
| """Mixin providing typed external accessor properties for Dataset.""" | ||
|
|
||
| __slots__ = () | ||
|
|
||
| hvplot: ClassVar[type[hvPlotAccessor]] | ||
| cf: ClassVar[type[CFAccessor]] | ||
| pint: ClassVar[type[PintDatasetAccessor]] | ||
| rio: ClassVar[type[RasterDataset]] | ||
| plotly: ClassVar[type[DatasetPlotlyAccessor]] | ||
|
|
||
|
|
||
| class DataTreeExternalAccessorMixin: | ||
| """Mixin providing typed external accessor properties for DataTree.""" | ||
|
|
||
| __slots__ = () | ||
|
|
||
| hvplot: ClassVar[type[hvPlotAccessor]] | ||
| cf: ClassVar[type[CFAccessor]] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just the mypy environmentThere was a problem hiding this comment.
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_CHECKINGblock. So we just need some explicit mypy ignores (which @FBumann had already figured out).There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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-plotlyinstalled, I only see the plotly accessor.But after I type in
.hv, also see that accessor with type hints.Completion after the accessor name is only available for installed accessors:
One more thing:

The data_vars overwrite the accessor. This is a good thing imo!
The existing

.plotaccessor behaves differently. It overwrites the data_varThis means with conflicting var_names and accessor names, the code behaviour is backwards compatible. Only conflict in type hints, not at Runtime.