Skip to content

Commit 0db5ff8

Browse files
authored
Address warnings emitted during test execution for deallocating cachefilemanager (#386)
1 parent e3787c5 commit 0db5ff8

File tree

5 files changed

+236
-188
lines changed

5 files changed

+236
-188
lines changed

ci/upstream.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies:
2121
- mypy
2222
- ruff
2323
- pandas-stubs
24+
- pytest-asyncio
2425
- pytest-mypy
2526
- pytest-cov
2627
- pytest

conftest.py

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Any, Dict, Optional
1+
from pathlib import Path
2+
from typing import Any, Callable, Mapping, Optional
23

34
import h5py
45
import numpy as np
@@ -25,74 +26,70 @@ def pytest_runtest_setup(item):
2526

2627

2728
@pytest.fixture
28-
def empty_netcdf4_file(tmpdir):
29+
def empty_netcdf4_file(tmp_path: Path) -> str:
30+
filepath = tmp_path / "empty.nc"
31+
2932
# Set up example xarray dataset
30-
ds = xr.Dataset() # Save it to disk as netCDF (in temporary directory)
31-
filepath = f"{tmpdir}/empty.nc"
32-
ds.to_netcdf(filepath, format="NETCDF4")
33-
ds.close()
33+
with xr.Dataset() as ds: # Save it to disk as netCDF (in temporary directory)
34+
ds.to_netcdf(filepath, format="NETCDF4")
3435

35-
return filepath
36+
return str(filepath)
3637

3738

3839
@pytest.fixture
39-
def netcdf4_file(tmpdir):
40-
# Set up example xarray dataset
41-
ds = xr.tutorial.open_dataset("air_temperature")
40+
def netcdf4_file(tmp_path: Path) -> str:
41+
filepath = tmp_path / "air.nc"
4242

43-
# Save it to disk as netCDF (in temporary directory)
44-
filepath = f"{tmpdir}/air.nc"
45-
ds.to_netcdf(filepath, format="NETCDF4")
46-
ds.close()
43+
# Set up example xarray dataset
44+
with xr.tutorial.open_dataset("air_temperature") as ds:
45+
# Save it to disk as netCDF (in temporary directory)
46+
ds.to_netcdf(filepath, format="NETCDF4")
4747

48-
return filepath
48+
return str(filepath)
4949

5050

5151
@pytest.fixture
52-
def netcdf4_file_with_data_in_multiple_groups(tmpdir):
53-
filepath = str(tmpdir / "test.nc")
52+
def netcdf4_file_with_data_in_multiple_groups(tmp_path: Path) -> str:
53+
filepath = tmp_path / "test.nc"
5454

5555
ds1 = xr.DataArray([1, 2, 3], name="foo").to_dataset()
5656
ds1.to_netcdf(filepath)
5757
ds2 = xr.DataArray([4, 5], name="bar").to_dataset()
5858
ds2.to_netcdf(filepath, group="subgroup", mode="a")
5959

60-
return filepath
60+
return str(filepath)
6161

6262

6363
@pytest.fixture
64-
def netcdf4_files_factory(tmpdir) -> callable:
64+
def netcdf4_files_factory(tmp_path: Path) -> Callable:
6565
def create_netcdf4_files(
66-
encoding: Optional[Dict[str, Dict[str, Any]]] = None,
66+
encoding: Optional[Mapping[str, Mapping[str, Any]]] = None,
6767
) -> tuple[str, str]:
68-
ds = xr.tutorial.open_dataset("air_temperature")
69-
70-
# Split dataset into two parts
71-
ds1 = ds.isel(time=slice(None, 1460))
72-
ds2 = ds.isel(time=slice(1460, None))
68+
filepath1 = tmp_path / "air1.nc"
69+
filepath2 = tmp_path / "air2.nc"
7370

74-
# Save datasets to disk as NetCDF in the temporary directory with the provided encoding
75-
filepath1 = f"{tmpdir}/air1.nc"
76-
filepath2 = f"{tmpdir}/air2.nc"
77-
ds1.to_netcdf(filepath1, encoding=encoding)
78-
ds2.to_netcdf(filepath2, encoding=encoding)
71+
with xr.tutorial.open_dataset("air_temperature") as ds:
72+
# Split dataset into two parts
73+
ds1 = ds.isel(time=slice(None, 1460))
74+
ds2 = ds.isel(time=slice(1460, None))
7975

80-
# Close datasets
81-
ds1.close()
82-
ds2.close()
76+
# Save datasets to disk as NetCDF in the temporary directory with the provided encoding
77+
ds1.to_netcdf(filepath1, encoding=encoding)
78+
ds2.to_netcdf(filepath2, encoding=encoding)
8379

84-
return filepath1, filepath2
80+
return str(filepath1), str(filepath2)
8581

8682
return create_netcdf4_files
8783

8884

8985
@pytest.fixture
90-
def netcdf4_file_with_2d_coords(tmpdir):
91-
ds = xr.tutorial.open_dataset("ROMS_example")
92-
filepath = f"{tmpdir}/ROMS_example.nc"
93-
ds.to_netcdf(filepath, format="NETCDF4")
94-
ds.close()
95-
return filepath
86+
def netcdf4_file_with_2d_coords(tmp_path: Path) -> str:
87+
filepath = tmp_path / "ROMS_example.nc"
88+
89+
with xr.tutorial.open_dataset("ROMS_example") as ds:
90+
ds.to_netcdf(filepath, format="NETCDF4")
91+
92+
return str(filepath)
9693

9794

9895
@pytest.fixture
@@ -110,44 +107,46 @@ def netcdf4_inlined_ref(netcdf4_file):
110107

111108

112109
@pytest.fixture
113-
def hdf5_groups_file(tmpdir):
114-
# Set up example xarray dataset
115-
ds = xr.tutorial.open_dataset("air_temperature")
110+
def hdf5_groups_file(tmp_path: Path) -> str:
111+
filepath = tmp_path / "air.nc"
116112

117-
# Save it to disk as netCDF (in temporary directory)
118-
filepath = f"{tmpdir}/air.nc"
119-
ds.to_netcdf(filepath, format="NETCDF4", group="test/group")
120-
ds.close()
113+
# Set up example xarray dataset
114+
with xr.tutorial.open_dataset("air_temperature") as ds:
115+
# Save it to disk as netCDF (in temporary directory)
116+
ds.to_netcdf(filepath, format="NETCDF4", group="test/group")
121117

122-
return filepath
118+
return str(filepath)
123119

124120

125121
@pytest.fixture
126-
def hdf5_empty(tmpdir):
127-
filepath = f"{tmpdir}/empty.nc"
128-
f = h5py.File(filepath, "w")
129-
dataset = f.create_dataset("empty", shape=(), dtype="float32")
130-
dataset.attrs["empty"] = "true"
131-
return filepath
122+
def hdf5_empty(tmp_path: Path) -> str:
123+
filepath = tmp_path / "empty.nc"
124+
125+
with h5py.File(filepath, "w") as f:
126+
dataset = f.create_dataset("empty", shape=(), dtype="float32")
127+
dataset.attrs["empty"] = "true"
128+
129+
return str(filepath)
132130

133131

134132
@pytest.fixture
135-
def hdf5_scalar(tmpdir):
136-
filepath = f"{tmpdir}/scalar.nc"
137-
f = h5py.File(filepath, "w")
138-
dataset = f.create_dataset("scalar", data=0.1, dtype="float32")
139-
dataset.attrs["scalar"] = "true"
140-
return filepath
133+
def hdf5_scalar(tmp_path: Path) -> str:
134+
filepath = tmp_path / "scalar.nc"
135+
136+
with h5py.File(filepath, "w") as f:
137+
dataset = f.create_dataset("scalar", data=0.1, dtype="float32")
138+
dataset.attrs["scalar"] = "true"
139+
140+
return str(filepath)
141141

142142

143143
@pytest.fixture
144-
def simple_netcdf4(tmpdir):
145-
filepath = f"{tmpdir}/simple.nc"
144+
def simple_netcdf4(tmp_path: Path) -> str:
145+
filepath = tmp_path / "simple.nc"
146146

147147
arr = np.arange(12, dtype=np.dtype("int32")).reshape(3, 4)
148148
var = Variable(data=arr, dims=["x", "y"])
149149
ds = xr.Dataset({"foo": var})
150-
151150
ds.to_netcdf(filepath)
152151

153-
return filepath
152+
return str(filepath)

pyproject.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,22 @@ line-ending = "auto"
149149
known-first-party = ["virtualizarr"]
150150

151151
[tool.pytest.ini_options]
152+
# See https://pytest-asyncio.readthedocs.io/en/latest/concepts.html#asyncio-event-loops
153+
# Explicitly set asyncio_default_fixture_loop_scope to eliminate the following warning:
154+
#
155+
# PytestDeprecationWarning: The configuration option "asyncio_default_fixture_loop_scope"
156+
# is unset. The event loop scope for asynchronous fixtures will default to the fixture
157+
# caching scope. Future versions of pytest-asyncio will default the loop scope for
158+
# asynchronous fixtures to function scope. Set the default fixture loop scope
159+
# explicitly in order to avoid unexpected behavior in the future. Valid fixture loop
160+
# scopes are: "function", "class", "module", "package", "session"
161+
#
162+
asyncio_default_fixture_loop_scope = "session"
152163
markers = [
164+
# Although we may not use pytest.mark.flaky, some of our test modules import
165+
# from xarray.tests, and xarray.tests.__init__.py references pytest.mark.flaky.
166+
# Therefore, without the "flaky" marker below, during test execution, we see
167+
# this warning: "PytestUnknownMarkWarning: Unknown pytest.mark.flaky"
168+
"flaky: flaky tests",
153169
"network: marks test requiring internet (select with '--run-network-tests')",
154170
]

0 commit comments

Comments
 (0)