Skip to content

Commit

Permalink
Invalid type of X ValueError lists unclear accepted types (#1395)
Browse files Browse the repository at this point in the history
* add module info to allowed types

* Update anndata/_core/anndata.py

Co-authored-by: Isaac Virshup <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added test

* release note and latest discussed msg string

---------

Co-authored-by: Isaac Virshup <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 5, 2024
1 parent 551287a commit 9452dd5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 1 addition & 2 deletions anndata/_core/anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,8 @@ def _init_as_actual(
if isinstance(X, s_type.value):
break
else:
class_names = ", ".join(c.__name__ for c in StorageType.classes())
raise ValueError(
f"`X` needs to be of one of {class_names}, not {type(X)}."
f"X needs to be of one of numpy.ndarray, numpy.ma.core.MaskedArray, scipy.sparse.spmatrix, h5py.Dataset, zarr.Array, anndata.experimental.[CSC,CSR]Dataset, dask.array.Array, cupy.ndarray, cupyx.scipy.sparse.spmatrix, not {type(X)}."
)
if shape is not None:
raise ValueError("`shape` needs to be `None` if `X` is not `None`.")
Expand Down
10 changes: 10 additions & 0 deletions anndata/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ def test_creation_error(src, src_arg, dim_msg, dim, dim_arg, msg: str | None):
AnnData(**{src: src_arg, dim: dim_arg(dim)})


def test_invalid_X():
with pytest.raises(
ValueError,
match=re.escape(
"X needs to be of one of numpy.ndarray, numpy.ma.core.MaskedArray, scipy.sparse.spmatrix, h5py.Dataset, zarr.Array, anndata.experimental.[CSC,CSR]Dataset, dask.array.Array, cupy.ndarray, cupyx.scipy.sparse.spmatrix, not <class 'str'>."
),
):
AnnData("string is not a valid X")


def test_create_with_dfs():
X = np.ones((6, 3))
obs = pd.DataFrame(dict(cat_anno=pd.Categorical(["a", "a", "a", "a", "b", "a"])))
Expand Down
1 change: 1 addition & 0 deletions docs/release-notes/0.10.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Bring optimization from {pr}`1233` to indexing on the whole `AnnData` object, not just the sparse dataset itself {pr}`1365` {user}`ilan-gold`
* Fix mean slice length checking to use improved performance when indexing backed sparse matrices with boolean masks along their major axis {pr}`1366` {user}`ilan-gold`
* Fixed overflow occurring when writing dask arrays with sparse chunks by always writing dask arrays with 64 bit indptr and indices, and adding an overflow check to `.append` method of sparse on disk structures {pr}`1348` {user}`ivirshup`
* Modified `ValueError` message for invalid `.X` during construction to show more helpful list instead of ambiguous `__name__` {pr}`1395` {user}`eroell`

```{rubric} Documentation
```
Expand Down

0 comments on commit 9452dd5

Please sign in to comment.