From cbd759c61c947411a0b7c31725334a929aa7d803 Mon Sep 17 00:00:00 2001 From: nishantharkut Date: Tue, 17 Mar 2026 13:58:42 +0530 Subject: [PATCH 1/2] fix: guard data_vars when no grid is recognized 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. --- tests/test_accessor.py | 17 +++++++++++++++++ xarray_subset_grid/accessor.py | 11 +++++------ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 tests/test_accessor.py diff --git a/tests/test_accessor.py b/tests/test_accessor.py new file mode 100644 index 0000000..7ec84d6 --- /dev/null +++ b/tests/test_accessor.py @@ -0,0 +1,17 @@ +import pytest +import xarray as xr + +import xarray_subset_grid.accessor # noqa: F401 + + +def test_data_vars_returns_empty_set_when_grid_not_recognized(): + ds = xr.Dataset( + data_vars={"foo": ("x", [1, 2, 3])}, + coords={"x": [0, 1, 2]}, + ) + + with pytest.warns(UserWarning, match="no grid type found in this dataset"): + accessor = ds.xsg + + assert accessor.grid is None + assert accessor.data_vars == set() diff --git a/xarray_subset_grid/accessor.py b/xarray_subset_grid/accessor.py index 7a4ca7a..cba1637 100644 --- a/xarray_subset_grid/accessor.py +++ b/xarray_subset_grid/accessor.py @@ -3,6 +3,7 @@ # from typing import Optional, Union import numpy as np import xarray as xr +from xarray.core.coordinates import DatasetCoordinates from xarray_subset_grid.grid import Grid from xarray_subset_grid.grids import ( @@ -78,18 +79,16 @@ def data_vars(self) -> set[str]: data analysis. These can be discarded when subsetting the dataset when they are not needed. """ - if self._ds: + if self._grid: return self._grid.data_vars(self._ds) return set() @property - def coords(self) -> set[str]: - if self._ds: - return self._ds.coords - return set() + def coords(self) -> DatasetCoordinates: + return self._ds.coords @property - def grid_vars(self) -> set[str]: + def grid_vars(self) -> set[str] | list[str]: """List of grid variables. These variables are used to define the grid and thus should be From bbfb6abc1dc63bc441cd7490d07addb46990e3bb Mon Sep 17 00:00:00 2001 From: nishantharkut Date: Tue, 17 Mar 2026 21:43:45 +0530 Subject: [PATCH 2/2] fix: address review feedback on accessor type annotations - 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 --- xarray_subset_grid/accessor.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/xarray_subset_grid/accessor.py b/xarray_subset_grid/accessor.py index cba1637..1b127fd 100644 --- a/xarray_subset_grid/accessor.py +++ b/xarray_subset_grid/accessor.py @@ -1,9 +1,9 @@ import warnings +from typing import TYPE_CHECKING # from typing import Optional, Union import numpy as np import xarray as xr -from xarray.core.coordinates import DatasetCoordinates from xarray_subset_grid.grid import Grid from xarray_subset_grid.grids import ( @@ -16,6 +16,9 @@ UGrid, ) +if TYPE_CHECKING: + from xarray.core.coordinates import DatasetCoordinates + _grid_impls = [ FVCOMGrid, SELFEGrid, @@ -84,11 +87,11 @@ def data_vars(self) -> set[str]: return set() @property - def coords(self) -> DatasetCoordinates: + def coords(self) -> "DatasetCoordinates": return self._ds.coords @property - def grid_vars(self) -> set[str] | list[str]: + def grid_vars(self) -> set[str]: """List of grid variables. These variables are used to define the grid and thus should be