diff --git a/apis/python/src/tiledbsoma/io/ingest.py b/apis/python/src/tiledbsoma/io/ingest.py index 8c6dc31f94..48c08146f2 100644 --- a/apis/python/src/tiledbsoma/io/ingest.py +++ b/apis/python/src/tiledbsoma/io/ingest.py @@ -39,6 +39,7 @@ from anndata._core import file_backing from anndata._core.sparse_dataset import SparseDataset from somacore.options import PlatformConfig +from typing_extensions import get_args from .. import ( Collection, @@ -421,6 +422,30 @@ def from_anndata( "Second argument is not an AnnData object -- did you want from_h5ad?" ) + for key in anndata.obsm.keys(): + if not isinstance(anndata.obsm[key], get_args(Matrix)): + raise TypeError( + f"obsm value at {key} in not of type {list(cl.__name__ for cl in get_args(Matrix))}" + ) + + for key in anndata.obsp.keys(): + if not isinstance(anndata.obsp[key], get_args(Matrix)): + raise TypeError( + f"obsp value at {key} in not of type {list(cl.__name__ for cl in get_args(Matrix))}" + ) + + for key in anndata.varm.keys(): + if not isinstance(anndata.varm[key], get_args(Matrix)): + raise TypeError( + f"varm value at {key} in not of type {list(cl.__name__ for cl in get_args(Matrix))}" + ) + + for key in anndata.varp.keys(): + if not isinstance(anndata.varp[key], get_args(Matrix)): + raise TypeError( + f"varp value at {key} in not of type {list(cl.__name__ for cl in get_args(Matrix))}" + ) + # For single ingest (no append): # # * obs, var, X, etc array indices map 1-1 to soma_joinid, soma_dim_0, soma_dim_1, etc. diff --git a/apis/python/tests/test_basic_anndata_io.py b/apis/python/tests/test_basic_anndata_io.py index 46ba266a78..4611194c89 100644 --- a/apis/python/tests/test_basic_anndata_io.py +++ b/apis/python/tests/test_basic_anndata_io.py @@ -1134,3 +1134,13 @@ def test_index_names_io(tmp_path, obs_index_name, var_index_name): assert bdata.var.index.name is None else: assert adata.var.index.name == bdata.var.index.name + +def test_obsm_data_type(adata): + tempdir = tempfile.TemporaryDirectory() + soma_path = tempdir.name + bdata = anndata.AnnData(X=adata.X, obs=adata.obs, var=adata.var, obsm={"testing": adata.obs}) + + with pytest.raises(TypeError): + tiledbsoma.io.from_anndata(soma_path, bdata, measurement_name="RNA") + + assert not any(Path(soma_path).iterdir())