Skip to content

Commit a4d707d

Browse files
authored
(fix): no asyncio.run for clearing a store (#1933)
* (fix): no `asyncio.run` for clearing a store * (chore): test * (chore): relnote
1 parent 098639e commit a4d707d

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

docs/release-notes/1933.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Don't use {func}`asyncio.run` internally for any operations {user}`ilan-gold`

src/anndata/_io/specs/registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,9 @@ def write_elem(
361361

362362
if k == "/":
363363
if isinstance(store, ZarrGroup) and not is_zarr_v2():
364-
import asyncio
364+
from zarr.core.sync import sync
365365

366-
asyncio.run(store.store.clear())
366+
sync(store.store.clear())
367367
else:
368368
store.clear()
369369
elif k in store:

tests/test_readwrite.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ def test_readwrite_roundtrip(typ, tmp_path, diskfmt, diskfmt2):
131131
assert_equal(adata2, adata1)
132132

133133

134+
def test_readwrite_roundtrip_async(tmp_path):
135+
import asyncio
136+
137+
async def _do_test():
138+
zarr_path = tmp_path / "first.zarr"
139+
140+
adata1 = ad.AnnData(
141+
csr_matrix(X_list), obs=obs_dict, var=var_dict, uns=uns_dict
142+
)
143+
adata1.write_zarr(zarr_path)
144+
adata2 = ad.read_zarr(zarr_path)
145+
146+
assert_equal(adata2, adata1)
147+
148+
# This test ensures our file i/o never calls `asyncio.run` internally
149+
asyncio.run(_do_test())
150+
151+
134152
@pytest.mark.parametrize("storage", ["h5ad", "zarr"])
135153
@pytest.mark.parametrize("typ", [np.array, csr_matrix, csr_array, as_dense_dask_array])
136154
def test_readwrite_kitchensink(tmp_path, storage, typ, backing_h5ad, dataset_kwargs):

0 commit comments

Comments
 (0)