Skip to content

Commit fa614ce

Browse files
committed
STYLE: Fix type checking errors in volumeutils.rec2dict
Fix type checking errors in `volumeutils.rec2dict`: - Ensure that the recarray is iterable; return immediately if no fields are found. - Accomodate `Any` type in return the dictionary type Fixes: ``` nibabel/volumeutils.py:1391: error: Item "None" of "MappingProxyType[str, tuple[dtype[Any], int] | tuple[dtype[Any], int, Any]] | None" has no attribute "__iter__" (not iterable) [union-attr] ``` and ``` nibabel/volumeutils.py:1398: error: Incompatible return value type (got "dict[str | Any, ndarray[tuple[Any, ...], dtype[Any]]]", expected "dict[str, generic[Any] | ndarray[tuple[Any, ...], dtype[Any]]]") [return-value] nibabel/volumeutils.py:1398: note: Perhaps you need a type annotation for "dct"? Suggestion: "dict[str, generic[Any] | ndarray[tuple[Any, ...], dtype[Any]]]" ``` raised for example in: https://github.com/nipy/nibabel/actions/runs/16092302666/job/45410655458?pr=1422#step:7:22
1 parent b5ecf48 commit fa614ce

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

nibabel/volumeutils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ def shape_zoom_affine(
13651365
return aff
13661366

13671367

1368-
def rec2dict(rec: np.ndarray) -> dict[str, np.generic | np.ndarray]:
1368+
def rec2dict(rec: np.ndarray) -> dict[str, Any]:
13691369
"""Convert recarray to dictionary
13701370
13711371
Also converts scalar values to scalars
@@ -1388,6 +1388,8 @@ def rec2dict(rec: np.ndarray) -> dict[str, np.generic | np.ndarray]:
13881388
True
13891389
"""
13901390
dct = {}
1391+
if rec.dtype.fields is None:
1392+
return dct
13911393
for key in rec.dtype.fields:
13921394
val = rec[key]
13931395
try:

0 commit comments

Comments
 (0)