Skip to content

Commit 26c5876

Browse files
committed
refactor: make _get_category_colors strict about AnnData type
1 parent 1a3c7e9 commit 26c5876

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/cellmapper/model/evaluate.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _get_category_colors(
3232
Parameters
3333
----------
3434
adata
35-
AnnData object to get colors from.
35+
AnnData object to get colors from (must be AnnData, not a list).
3636
label_key
3737
Key in .obs storing the categorical annotation.
3838
categories
@@ -41,11 +41,20 @@ def _get_category_colors(
4141
Returns
4242
-------
4343
List of colors corresponding to each category.
44+
45+
Raises
46+
------
47+
TypeError
48+
If adata is not None and not an AnnData object.
4449
"""
50+
if adata is not None and not hasattr(adata, "uns"):
51+
msg = f"Expected AnnData object, got {type(adata).__name__}."
52+
raise TypeError(msg)
53+
4554
colors_key = f"{label_key}_colors"
4655
colors_dict: dict[str, str] = {}
4756

48-
if adata is not None and hasattr(adata, "uns") and colors_key in adata.uns:
57+
if adata is not None and colors_key in adata.uns:
4958
full_categories = adata.obs[label_key].cat.categories
5059
full_colors = adata.uns[colors_key]
5160
for i, cat in enumerate(full_categories):

0 commit comments

Comments
 (0)