From 2011a1ed2312d0a475313075dbc99a0d0fb81825 Mon Sep 17 00:00:00 2001 From: "Lumberbot (aka Jack)" <39504233+meeseeksmachine@users.noreply.github.com> Date: Mon, 24 Jul 2023 06:18:39 -0700 Subject: [PATCH] Backport PR #1040: Fix for awkward 2.3 (#1069) Co-authored-by: Isaac Virshup --- anndata/_core/views.py | 4 ++-- anndata/tests/test_awkward.py | 14 +++++++++++++- docs/release-notes/0.9.2.md | 1 + pyproject.toml | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/anndata/_core/views.py b/anndata/_core/views.py index 722ef9098..8111a4334 100644 --- a/anndata/_core/views.py +++ b/anndata/_core/views.py @@ -276,7 +276,7 @@ def __copy__(self) -> AwkArray: array = self # makes a shallow copy and removes the reference to the original AnnData object array = ak.with_parameter(self, _PARAM_NAME, None) - array = ak.with_parameter(array, "__array__", None) + array = ak.with_parameter(array, "__list__", None) return array @as_view.register(AwkArray) @@ -294,7 +294,7 @@ def as_view_awkarray(array, view_args): "Please open an issue in the AnnData repo and describe your use-case." ) array = ak.with_parameter(array, _PARAM_NAME, (parent_key, attrname, keys)) - array = ak.with_parameter(array, "__array__", "AwkwardArrayView") + array = ak.with_parameter(array, "__list__", "AwkwardArrayView") return array ak.behavior["AwkwardArrayView"] = AwkwardArrayView diff --git a/anndata/tests/test_awkward.py b/anndata/tests/test_awkward.py index 97c7c42f2..87280d5a2 100644 --- a/anndata/tests/test_awkward.py +++ b/anndata/tests/test_awkward.py @@ -147,8 +147,20 @@ def test_view(key): def test_view_of_awkward_array_with_custom_behavior(): """Currently can't create view of arrays with custom __name__ (in this case "string") See https://github.com/scverse/anndata/pull/647#discussion_r963494798_""" + + from uuid import uuid4 + + BEHAVIOUR_ID = str(uuid4()) + + class ReversibleArray(ak.Array): + def reversed(self): + return self[..., ::-1] + + ak.behavior[BEHAVIOUR_ID] = ReversibleArray adata = gen_adata((3, 3), varm_types=(), obsm_types=(), layers_types=()) - adata.obsm["awk_string"] = ak.Array(["AAA", "BBB", "CCC"]) + adata.obsm["awk_string"] = ak.with_parameter( + ak.Array(["AAA", "BBB", "CCC"]), "__list__", BEHAVIOUR_ID + ) adata_view = adata[:2] with pytest.raises(NotImplementedError): diff --git a/docs/release-notes/0.9.2.md b/docs/release-notes/0.9.2.md index 9e9ea6f7a..4422185e4 100644 --- a/docs/release-notes/0.9.2.md +++ b/docs/release-notes/0.9.2.md @@ -3,6 +3,7 @@ ```{rubric} Bugfix ``` +* Views of `awkward.Array`s now work with `awkward>=2.3` {pr}`1040` {user}`ivirshup` * Fix ufuncs of views like `adata.X[:10].cov(axis=0)` returning views {pr}`1043` {user}`flying-sheep` * Fix instantiating AnnData where `.X` is a `DataFrame` with an integer valued index {pr}`1002` {user}`flying-sheep` * Fix {func}`~anndata.read_zarr` when used on `zarr.Group` {pr}`1057` {user}`ivirshup` diff --git a/pyproject.toml b/pyproject.toml index 8745a110d..1a20bde95 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,7 +87,7 @@ test = [ "boltons", "scanpy", "dask[array]", - "awkward>=2.0.8,<2.3", + "awkward>=2.3", "pytest_memray", ]