Skip to content

Commit 389117b

Browse files
committed
TYP: Annotate pretty_mapping
1 parent 5e388c6 commit 389117b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

nibabel/volumeutils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import typing as ty
1515
import warnings
1616
from functools import reduce
17-
from operator import mul
17+
from operator import getitem, mul
1818
from os.path import exists, splitext
1919

2020
import numpy as np
@@ -26,6 +26,10 @@
2626

2727
pyzstd, HAVE_ZSTD, _ = optional_package('pyzstd')
2828

29+
if ty.TYPE_CHECKING: # pragma: no cover
30+
K = ty.TypeVar('K')
31+
V = ty.TypeVar('V')
32+
2933
sys_is_le = sys.byteorder == 'little'
3034
native_code = sys_is_le and '<' or '>'
3135
swapped_code = sys_is_le and '>' or '<'
@@ -283,7 +287,10 @@ def __getitem__(self, key: ty.Hashable) -> ty.Hashable:
283287
raise KeyError(key)
284288

285289

286-
def pretty_mapping(mapping, getterfunc=None):
290+
def pretty_mapping(
291+
mapping: ty.Mapping[K, V],
292+
getterfunc: ty.Callable[[ty.Mapping[K, V], K], V] | None = None,
293+
) -> str:
287294
"""Make pretty string from mapping
288295
289296
Adjusts text column to print values on basis of longest key.
@@ -332,9 +339,8 @@ def pretty_mapping(mapping, getterfunc=None):
332339
longer_field : method string
333340
"""
334341
if getterfunc is None:
335-
getterfunc = lambda obj, key: obj[key]
336-
lens = [len(str(name)) for name in mapping]
337-
mxlen = np.max(lens)
342+
getterfunc = getitem
343+
mxlen = max(len(str(name)) for name in mapping)
338344
fmt = '%%-%ds : %%s' % mxlen
339345
out = []
340346
for name in mapping:

0 commit comments

Comments
 (0)