Skip to content

Commit 743180b

Browse files
authored
refactor(spm): use clearer error message when getting data field dtype from dataset (#132)
* refactor(spm): use clearer error message when getting data field dtype from dataset, fix test cs client args when env present, improve dtype types map
1 parent 9b7ce0a commit 743180b

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

cryosparc/api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Any, Callable, ClassVar, Dict, Iterator, List, Optional, Tuple, TypedDict, Union
88

99
import httpx
10+
from pydantic import BaseModel
1011

1112
from . import registry
1213
from .errors import APIError
@@ -392,9 +393,9 @@ def _decode_json_response(value: Any, schema: dict):
392393
return model_class(value)
393394
elif model_class and issubclass(model_class, dict): # typed dict
394395
return model_class(**value)
395-
elif model_class: # pydantic model
396+
elif model_class and issubclass(model_class, BaseModel): # pydantic model
396397
# use model_validate in case validator result derives from subtype, e.g., Event model
397-
return model_class.model_validate(value) # type: ignore
398+
return model_class.model_validate(value)
398399
warnings.warn(
399400
f"[API] Warning: Received API response with unregistered schema type {schema['$ref']}. "
400401
"Returning as plain object."

cryosparc/dataset/dtype.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class DatasetHeader(TypedDict):
3535
"""Field names that require decompression."""
3636

3737

38-
DSET_TO_TYPE_MAP: Dict[DsetType, Type] = {
38+
DSET_TO_TYPE_MAP: Dict[DsetType, Type[Union[n.number, n.object_]]] = {
3939
DsetType.T_F32: n.float32,
4040
DsetType.T_F64: n.float64,
4141
DsetType.T_C32: n.complex64,
@@ -107,8 +107,10 @@ def get_data_field(data: Data, field: str) -> Field:
107107

108108
def get_data_field_dtype(data: Data, field: str) -> "DTypeLike":
109109
t = data.type(field)
110-
if t == 0 or t not in DSET_TO_TYPE_MAP:
111-
raise KeyError(f"Unknown dataset field {field} or field type {t}")
110+
if t == 0:
111+
raise KeyError(f"Unknown dataset field {field}")
112+
elif t not in DSET_TO_TYPE_MAP:
113+
raise KeyError(f"Unknown dataset field type {t}")
112114
dt = n.dtype(DSET_TO_TYPE_MAP[t])
113115
shape = data.getshp(field)
114116
return (dt.str, shape) if shape else dt.str

tests/conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,13 @@ def mock_api_client_class(mock_user, monkeypatch):
255255

256256
@pytest.fixture
257257
def cs(mock_api_client_class):
258-
return CryoSPARC("https://cryosparc.example.com", email="structura@example.com", password="password")
258+
return CryoSPARC(
259+
"https://cryosparc.example.com",
260+
email="structura@example.com",
261+
password="password",
262+
host=None,
263+
base_port=None,
264+
)
259265

260266

261267
@pytest.fixture

tests/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_cli_login(mock_api_client_class, mock_auth_path):
3131

3232

3333
def test_cli_login_auth(mock_user, mock_api_client_class, mock_auth_path):
34-
cs = CryoSPARC("https://cryosparc.example.com", email="structura@example.com")
34+
cs = CryoSPARC("https://cryosparc.example.com", email="structura@example.com", host=None, base_port=None)
3535
mock_api_client_class.__call__.assert_called_with(auth="abc123") # called with token
3636
assert cs.user == mock_user
3737
assert cs.test_connection()

0 commit comments

Comments
 (0)