Skip to content

Commit 2ecb617

Browse files
committed
Fix some warnings raised in the tests with xarray 2023.09.0
xarray now explicitly complains if you're doing Bad Things, instead of silently trying to work with it. Explicit warnings are good! But now I have to not do those Bad Things. This has the upside of removing one of the warnings the tests silenced, which can only be a good thing.
1 parent 0fd8308 commit 2ecb617

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

tests/conventions/test_cfgrid1d.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def make_dataset(
8686
# EMS fails to parse. There is no way around this using xarray natively,
8787
# you have to adjust it with nctool after saving it.
8888
time.encoding["units"] = "days since 1990-01-01 00:00:00 +10"
89+
# This can not be represented as an int, so explicitly set the dtype.
90+
# xarray >=2023.09 warns when encoding variables without a dtype
91+
# that can not be represented exactly.
92+
time.encoding["dtype"] = "float32"
8993

9094
botz = xarray.DataArray(
9195
data=numpy.random.random((height, width)) * 10 + 50,

tests/conventions/test_cfgrid2d.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ def make_dataset(
9494
"coordinate_type": "time",
9595
},
9696
)
97+
# Note: xarray will reformat this in to 1990-01-01T00:00:00+10:00, which
98+
# EMS fails to parse. There is no way around this using xarray natively,
99+
# you have to adjust it with nctool after saving it.
100+
time.encoding["units"] = "days since 1990-01-01 00:00:00 +10"
101+
# This can not be represented as an int, so explicitly set the dtype.
102+
# xarray >=2023.09 warns when encoding variables without a dtype
103+
# that can not be represented exactly.
104+
time.encoding["dtype"] = "float32"
97105

98106
botz = xarray.DataArray(
99107
data=numpy.random.random((j_size, i_size)) * 10 + 50,

tests/conventions/test_shoc_standard.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def make_dataset(
9898
# EMS fails to parse. There is no way around this using xarray natively,
9999
# you have to adjust it with nctool after saving it.
100100
t.encoding["units"] = "days since 1990-01-01 00:00:00 +10"
101+
# This can not be represented as an int, so explicitly set the dtype.
102+
# xarray >=2023.09 warns when encoding variables without a dtype
103+
# that can not be represented exactly.
104+
t.encoding["dtype"] = "float32"
101105

102106
botz = xarray.DataArray(
103107
data=numpy.random.random((j_size, i_size)) * 10 + 50,

tests/conventions/test_ugrid.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ def make_dataset(
206206
# EMS fails to parse. There is no way around this using xarray natively,
207207
# you have to adjust it with nctool after saving it.
208208
t.encoding["units"] = "days since 1990-01-01 00:00:00 +10"
209+
# This can not be represented as an int, so explicitly set the dtype.
210+
# xarray >=2023.09 warns when encoding variables without a dtype
211+
# that can not be represented exactly.
212+
t.encoding["dtype"] = "float32"
209213

210214
botz = xarray.DataArray(
211215
data=numpy.random.random(cell_size) * 10 + 50,

tests/masking/test_mask_dataset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def test_mask_dataset(tmp_path: pathlib.Path):
136136
},
137137
)
138138
t.encoding["units"] = "days since 1990-01-01 00:00:00 +10"
139+
t.encoding["dtype"] = numpy.dtype("float32")
139140

140141
botz_missing_value = numpy.float32(-99.)
141142
botz = xarray.DataArray(

tests/test_utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import logging
33
import pathlib
4+
from importlib.metadata import version
45

56
import netCDF4
67
import numpy
@@ -9,13 +10,17 @@
910
import pytest
1011
import xarray
1112
import xarray.testing
13+
from packaging.version import parse
1214

1315
from emsarray import utils
1416
from tests.utils import filter_warning
1517

1618
logger = logging.getLogger(__name__)
1719

1820

21+
xarray_version = parse(version('xarray'))
22+
23+
1924
@pytest.mark.parametrize(
2025
('existing', 'new'),
2126
[
@@ -87,6 +92,7 @@ def test_disable_default_fill_value(tmp_path: pathlib.Path):
8792
data=td_data, dims=['j', 'i'])
8893
timedelta_with_missing_value_var.encoding['missing_value'] = numpy.float64('1e35')
8994
timedelta_with_missing_value_var.encoding['units'] = 'days'
95+
timedelta_with_missing_value_var.encoding['dtype'] = numpy.dtype('float64')
9096

9197
dataset = xarray.Dataset(data_vars={
9298
"int_var": int_var,
@@ -96,7 +102,7 @@ def test_disable_default_fill_value(tmp_path: pathlib.Path):
96102
})
97103

98104
# Save to a netCDF4 and then prove that it is bad
99-
# This emits warnings in current versions of xarray / numpy.
105+
# This emits warnings in older versions of xarray / numpy.
100106
# See https://github.com/pydata/xarray/issues/7942
101107
with filter_warning(
102108
'default', category=RuntimeWarning,
@@ -105,7 +111,10 @@ def test_disable_default_fill_value(tmp_path: pathlib.Path):
105111
record=True,
106112
) as ws:
107113
dataset.to_netcdf(tmp_path / "bad.nc")
108-
assert len(ws) == 1
114+
if xarray_version >= parse('2023.09'):
115+
assert len(ws) == 0
116+
else:
117+
assert len(ws) == 1
109118

110119
with netCDF4.Dataset(tmp_path / "bad.nc", "r") as nc_dataset:
111120
# This one shouldn't be here because it is an integer datatype. xarray
@@ -116,6 +125,8 @@ def test_disable_default_fill_value(tmp_path: pathlib.Path):
116125
assert numpy.isnan(nc_dataset.variables["float_var"].getncattr("_FillValue"))
117126
# This one is quite alright, we did explicitly set it after all
118127
assert numpy.isnan(nc_dataset.variables["float_with_fill_value_var"].getncattr("_FillValue"))
128+
# Same with this one
129+
assert nc_dataset.variables["timedelta_with_missing_value_var"].getncattr("missing_value") == numpy.float64('1e35')
119130
# This one is incorrect, a `missing_value` attribute has already been set
120131
assert numpy.isnan(nc_dataset.variables["timedelta_with_missing_value_var"].getncattr("_FillValue"))
121132

@@ -129,7 +140,10 @@ def test_disable_default_fill_value(tmp_path: pathlib.Path):
129140
record=True,
130141
) as ws:
131142
dataset.to_netcdf(tmp_path / "good.nc")
132-
assert len(ws) == 1
143+
if xarray_version >= parse('2023.09'):
144+
assert len(ws) == 0
145+
else:
146+
assert len(ws) == 1
133147

134148
with netCDF4.Dataset(tmp_path / "good.nc", "r") as nc_dataset:
135149
# This one should still be unset

0 commit comments

Comments
 (0)