Skip to content

fix: check self._grid instead of self._ds in data_vars property#121

Open
nishantharkut wants to merge 2 commits intoioos:mainfrom
nishantharkut:fix/accessor-data-vars-guard
Open

fix: check self._grid instead of self._ds in data_vars property#121
nishantharkut wants to merge 2 commits intoioos:mainfrom
nishantharkut:fix/accessor-data-vars-guard

Conversation

@nishantharkut
Copy link

Summary

This PR fixes the accessor guard used by data_vars when no grid implementation is recognized for a dataset.

Problem

The data_vars property checked self._ds, which is always present after accessor initialization. In no-grid cases, that guard still allowed a call through self._grid, which can be None.

Changes

  1. Update data_vars guard to check self._grid before delegating.
  2. Add a focused regression test for datasets where no grid type is recognized.
  3. Align nearby accessor return annotations with actual return types to resolve return-type diagnostics in editor/type checking.

Validation

  1. pre-commit hooks pass on changed files.
  2. Targeted regression and related grid accessor tests pass locally.

Check self._grid before delegating in the accessor data_vars property so unsupported datasets return an empty set instead of calling through a missing grid implementation. Add a focused regression test for the no-grid path, and align nearby accessor return annotations with actual return types to clear return-type diagnostics.
Copilot AI review requested due to automatic review settings March 17, 2026 09:51
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes the GridDatasetAccessor.data_vars guard so it doesn’t attempt to delegate through self._grid when no grid implementation is recognized, and adds a regression test for the no-grid scenario.

Changes:

  • Fix data_vars to check self._grid (not self._ds) before delegating to the grid implementation.
  • Add a regression test asserting data_vars returns an empty set when no grid type is recognized (and a warning is emitted).
  • Update nearby accessor return annotations to better reflect actual return types.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
xarray_subset_grid/accessor.py Fixes no-grid guard in data_vars and updates accessor typing for coords/grid_vars.
tests/test_accessor.py Adds a focused regression test for the no-grid accessor case.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 6 to 7
from xarray.core.coordinates import DatasetCoordinates

Comment on lines 91 to 92
def grid_vars(self) -> set[str] | list[str]:
"""List of grid variables.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the type annotation suggestion is good -- let's make it always a set

but is this line reqwuired?

set(self._grid.grid_vars(self._ds))

probably better to make sure that _grid._grid_vars is already a set.

- Move DatasetCoordinates import behind TYPE_CHECKING to avoid
  runtime dependency on private xarray.core module
- Revert grid_vars annotation to set[str] to match actual return types
@ChrisBarker-NOAA
Copy link
Contributor

Sorry I haven't had time to review these -- this looks good at a glance, but I do want to look closer before merging.

A note about type hints -- they are in there, but I'm not very experienced with them.

But as a philosophy, I prefer type hints not to be too restrictive -- e.g. don't type it a s list if it can be and Sequence -- I have no idea how well that's been done in this project so far, but something to keep in mind.

@nishantharkut
Copy link
Author

Thanks for the review ! I made the second commit (bbfb6ab) that reverts grid_vars back to set[str] and moves the DatasetCoordinates import behind TYPE_CHECKING which are both based on the Copilot suggestions. The only annotation change remaining is coords, which was returning DatasetCoordinates but annotated as set[str].

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants