From 1ed2b6ca23729d36d9977b53622ae344e6b63b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Sat, 5 Jul 2025 18:04:54 -0400 Subject: [PATCH] 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 --- nibabel/volumeutils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/nibabel/volumeutils.py b/nibabel/volumeutils.py index 41bff7275..e3a767beb 100644 --- a/nibabel/volumeutils.py +++ b/nibabel/volumeutils.py @@ -16,6 +16,7 @@ from functools import reduce from operator import getitem, mul from os.path import exists, splitext +from typing import Any import numpy as np @@ -1365,7 +1366,7 @@ def shape_zoom_affine( return aff -def rec2dict(rec: np.ndarray) -> dict[str, np.generic | np.ndarray]: +def rec2dict(rec: np.ndarray) -> dict[str, Any]: """Convert recarray to dictionary Also converts scalar values to scalars @@ -1387,7 +1388,9 @@ def rec2dict(rec: np.ndarray) -> dict[str, np.generic | np.ndarray]: >>> d == {'x': 0, 's': b''} True """ - dct = {} + dct: dict[str, Any] = {} + if rec.dtype.fields is None: + return dct for key in rec.dtype.fields: val = rec[key] try: