From 1d287c6cb1ba53d7427756135007e57eaf16c1b8 Mon Sep 17 00:00:00 2001 From: Kkkakania <200867803+Kkkakania@users.noreply.github.com> Date: Fri, 24 Jul 2026 11:27:41 +0800 Subject: [PATCH] Fix custom criteria membership checks --- cf_xarray/accessor.py | 6 ++++++ cf_xarray/tests/test_accessor.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index ed0c216d..fe828099 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -2039,6 +2039,12 @@ def keys(self) -> set[Hashable]: varnames.extend(list(self.cell_measures)) varnames.extend(list(self.standard_names)) varnames.extend(list(self.cf_roles)) + custom_criteria = ChainMap(*OPTIONS["custom_criteria"]) + varnames.extend( + key + for key in custom_criteria + if _get_custom_criteria(self._obj, key, custom_criteria) + ) if isinstance(self._obj, xr.Dataset): varnames.extend(list(self.grid_mapping_names)) else: diff --git a/cf_xarray/tests/test_accessor.py b/cf_xarray/tests/test_accessor.py index 6657be46..cbd9338a 100644 --- a/cf_xarray/tests/test_accessor.py +++ b/cf_xarray/tests/test_accessor.py @@ -2174,6 +2174,20 @@ def test_custom_criteria() -> None: assert_identical(ds.cf["temp"], ds["temperature"]) +def test_custom_criteria_membership() -> None: + custom_criteria = { + "salt": {"standard_name": "salinity"}, + "ssh": {"standard_name": "sea_surface_height"}, + } + ds = xr.Dataset({"salinity": ("time", [34.5, 35.0], {"standard_name": "salinity"})}) + + with cf_xarray.set_options(custom_criteria=custom_criteria): + assert "salt" in ds.cf.keys() + assert "salt" in ds.cf + assert "ssh" not in ds.cf.keys() + assert "ssh" not in ds.cf + + @requires_regex def test_regex_match(): # test that having a global regex expression flag later in the expression will work if