Skip to content

Commit 0e7f41c

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 0e7f41c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

nibabel/volumeutils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from functools import reduce
1717
from operator import getitem, mul
1818
from os.path import exists, splitext
19+
from typing import Any
1920

2021
import numpy as np
2122

@@ -1365,7 +1366,7 @@ def shape_zoom_affine(
13651366
return aff
13661367

13671368

1368-
def rec2dict(rec: np.ndarray) -> dict[str, np.generic | np.ndarray]:
1369+
def rec2dict(rec: np.ndarray) -> dict[str, Any]:
13691370
"""Convert recarray to dictionary
13701371
13711372
Also converts scalar values to scalars
@@ -1388,6 +1389,8 @@ def rec2dict(rec: np.ndarray) -> dict[str, np.generic | np.ndarray]:
13881389
True
13891390
"""
13901391
dct = {}
1392+
if rec.dtype.fields is None:
1393+
return dct
13911394
for key in rec.dtype.fields:
13921395
val = rec[key]
13931396
try:

0 commit comments

Comments
 (0)