From f9179800c1d0aad423b0e3aa950aaeb5185ad395 Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Fri, 5 Dec 2025 16:57:35 -0500 Subject: [PATCH 1/5] Dont preserve attrs when creating bounds --- cf_xarray/accessor.py | 22 +++++++++++----------- cf_xarray/tests/test_accessor.py | 3 +++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index 21feb6ff..348af0c3 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -796,18 +796,18 @@ def _guess_bounds(da, dim=None, out_dim="bounds"): f"If dim is None, variable {da.name} must be 1D or 2D. Received {da.ndim}D variable instead." ) dim = da.dims + with xr.set_options(keep_attrs=False): + if not isinstance(dim, str): + if len(dim) > 2: + raise NotImplementedError( + "Adding bounds with more than 2 dimensions is not supported." + ) + elif len(dim) == 2: + return _guess_bounds_2d(da, dim).rename(bounds=out_dim) + else: + dim = dim[0] - if not isinstance(dim, str): - if len(dim) > 2: - raise NotImplementedError( - "Adding bounds with more than 2 dimensions is not supported." - ) - elif len(dim) == 2: - return _guess_bounds_2d(da, dim).rename(bounds=out_dim) - else: - dim = dim[0] - - return _guess_bounds_1d(da, dim).rename(bounds=out_dim) + return _guess_bounds_1d(da, dim).rename(bounds=out_dim) def _build_docstring(func): diff --git a/cf_xarray/tests/test_accessor.py b/cf_xarray/tests/test_accessor.py index 3b65fbc6..58a811bf 100644 --- a/cf_xarray/tests/test_accessor.py +++ b/cf_xarray/tests/test_accessor.py @@ -832,6 +832,9 @@ def test_add_bounds(dims): assert_allclose( added[name].reset_coords(drop=True), expected[dim].transpose(..., "bounds") ) + if dim == "lat": + # The CF axes shouldn't have changed + assert added.cf.axes["Y"] == ["lat"] _check_unchanged(original, ds) From ec86e329a6cbf5aea772e247f6c30b7e4bd4cd6c Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Fri, 5 Dec 2025 17:23:31 -0500 Subject: [PATCH 2/5] Explicit drop attrs where needed --- cf_xarray/accessor.py | 21 ++++++++++----------- cf_xarray/helpers.py | 2 +- pyproject.toml | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index 348af0c3..74c89145 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -796,18 +796,17 @@ def _guess_bounds(da, dim=None, out_dim="bounds"): f"If dim is None, variable {da.name} must be 1D or 2D. Received {da.ndim}D variable instead." ) dim = da.dims - with xr.set_options(keep_attrs=False): - if not isinstance(dim, str): - if len(dim) > 2: - raise NotImplementedError( - "Adding bounds with more than 2 dimensions is not supported." - ) - elif len(dim) == 2: - return _guess_bounds_2d(da, dim).rename(bounds=out_dim) - else: - dim = dim[0] + if not isinstance(dim, str): + if len(dim) > 2: + raise NotImplementedError( + "Adding bounds with more than 2 dimensions is not supported." + ) + elif len(dim) == 2: + return _guess_bounds_2d(da, dim).rename(bounds=out_dim) + else: + dim = dim[0] - return _guess_bounds_1d(da, dim).rename(bounds=out_dim) + return _guess_bounds_1d(da, dim) def _build_docstring(func): diff --git a/cf_xarray/helpers.py b/cf_xarray/helpers.py index 1053a55b..747f111c 100644 --- a/cf_xarray/helpers.py +++ b/cf_xarray/helpers.py @@ -34,7 +34,7 @@ def _guess_bounds_1d(da, dim): result = xr.concat([first, bounds], dim=dim).transpose(..., "bounds") if ADDED_INDEX: result = result.drop_vars(dim) - return result + return result.drop_attrs(deep=False) def _guess_bounds_2d(da, dims): diff --git a/pyproject.toml b/pyproject.toml index a496e8b1..c2a40f4b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -154,7 +154,7 @@ omit = ["*/tests/*"] [dependency-groups] test-base = [ "pytest-cov", - "pytest", + "pytest<9", "pytest-xdist", "pytest-pretty", ] From 3fdf1508bfb077c8637a3ee9907cc944ffbb469d Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Fri, 5 Dec 2025 17:24:26 -0500 Subject: [PATCH 3/5] revert unwanted removal --- cf_xarray/accessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index 74c89145..602801c3 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -806,7 +806,7 @@ def _guess_bounds(da, dim=None, out_dim="bounds"): else: dim = dim[0] - return _guess_bounds_1d(da, dim) + return _guess_bounds_1d(da, dim).rename(bounds=out_dim) def _build_docstring(func): From c8440bf02a73567ddebf6812cd91993249b8c3ba Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Tue, 9 Dec 2025 14:47:47 -0500 Subject: [PATCH 4/5] fix mypy issues --- cf_xarray/geometry.py | 6 +++++- cf_xarray/units.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cf_xarray/geometry.py b/cf_xarray/geometry.py index 6e4f1090..442e632b 100644 --- a/cf_xarray/geometry.py +++ b/cf_xarray/geometry.py @@ -586,7 +586,11 @@ def points_to_cf( coord = None pts_ = pts - x, y, node_count, crdX, crdY = [], [], [], [], [] + x: list[np.ndarray] = [] + y: list[np.ndarray] = [] + node_count: list[int] = [] + crdX: list[float] = [] + crdY: list[float] = [] for pt in pts_: if isinstance(pt, MultiPoint): xy = np.concatenate([p.coords for p in pt.geoms]) diff --git a/cf_xarray/units.py b/cf_xarray/units.py index e1d553e8..7f0fb281 100644 --- a/cf_xarray/units.py +++ b/cf_xarray/units.py @@ -60,7 +60,7 @@ def short_formatter(unit, registry, **options): # Reused with modification from MetPy under the terms of the BSD 3-Clause License. # Copyright (c) 2015,2017,2019 MetPy Developers. # Create registry, with preprocessors for UDUNITS-style powers (m2 s-2) and percent signs -units = pint.UnitRegistry( +units: pint.UnitRegistry = pint.UnitRegistry( autoconvert_offset_to_baseunit=True, preprocessors=[ functools.partial( From 34c6b08db0468b72283c0bd790b987f3581bbe58 Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Tue, 9 Dec 2025 14:57:23 -0500 Subject: [PATCH 5/5] Remove pytest config that is unnecessary and changed in pytest 9 - remove pin --- pyproject.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c2a40f4b..ad90d566 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -107,7 +107,6 @@ docstring-code-format = true [tool.pytest] -python_files = "test_*.py" testpaths = ["cf_xarray/tests"] [tool.rstcheck] @@ -154,7 +153,7 @@ omit = ["*/tests/*"] [dependency-groups] test-base = [ "pytest-cov", - "pytest<9", + "pytest", "pytest-xdist", "pytest-pretty", ]