11import datetime
22import logging
33import pathlib
4+ from importlib .metadata import version
45
56import netCDF4
67import numpy
910import pytest
1011import xarray
1112import xarray .testing
13+ from packaging .version import parse
1214
1315from emsarray import utils
1416from tests .utils import filter_warning
1517
1618logger = 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