Skip to content

Commit b91b697

Browse files
committed
RF: Update Gifti/Cifti2MetaData checks to _sanitize, add doctests
1 parent 11d2a21 commit b91b697

File tree

2 files changed

+69
-21
lines changed

2 files changed

+69
-21
lines changed

nibabel/cifti2/cifti2.py

+36-8
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,51 @@ class Cifti2MetaData(CaretMetaData):
122122
----------
123123
data : list of (name, value) tuples
124124
"""
125-
def __init__(self, *args, **kwargs):
125+
@staticmethod
126+
def _sanitize(args, kwargs):
127+
""" Sanitize and warn on deprecated arguments
128+
129+
Accept metadata positional/keyword argument that can take
130+
``None`` to indicate no initialization.
131+
132+
>>> import pytest
133+
>>> Cifti2MetaData._sanitize((), {})
134+
((), {})
135+
>>> Cifti2MetaData._sanitize(([("key", "val")],), {})
136+
(([('key', 'val')],), {})
137+
>>> Cifti2MetaData._sanitize((), {"key": "val"})
138+
((), {'key': 'val'})
139+
>>> with pytest.warns(FutureWarning):
140+
... Cifti2MetaData._sanitize((None,), {})
141+
((), {})
142+
>>> with pytest.warns(FutureWarning):
143+
... Cifti2MetaData._sanitize((), {'metadata': None})
144+
((), {})
145+
>>> with pytest.warns(FutureWarning):
146+
... Cifti2MetaData._sanitize((), {'metadata': {'key': 'val'}})
147+
(({'key': 'val'},), {})
148+
149+
Note that "metadata" could be a valid key:
150+
151+
>>> Cifti2MetaData._sanitize((), {'metadata': 'val'})
152+
((), {'metadata': 'val'})
153+
"""
126154
if not args and list(kwargs) == ["metadata"]:
127-
md = kwargs.pop("metadata")
128-
if not isinstance(md, str):
129-
warn("CaretMetaData now has a dict-like interface and will "
155+
if not isinstance(kwargs["metadata"], str):
156+
warn("Cifti2MetaData now has a dict-like interface and will "
130157
"no longer accept the ``metadata`` keyword argument in "
131158
"NiBabel 6.0. See ``pydoc dict`` for initialization options.",
132-
FutureWarning, stacklevel=2)
159+
FutureWarning, stacklevel=3)
160+
md = kwargs.pop("metadata")
133161
if md is not None:
134162
args = (md,)
135163
if args == (None,):
136-
warn("CaretMetaData now has a dict-like interface and will no longer "
164+
warn("Cifti2MetaData now has a dict-like interface and will no longer "
137165
"accept the positional argument ``None`` in NiBabel 6.0. "
138166
"See ``pydoc dict`` for initialization options.",
139-
FutureWarning, stacklevel=2)
167+
FutureWarning, stacklevel=3)
140168
args = ()
141-
super().__init__(*args, **kwargs)
169+
return args, kwargs
142170

143171
@property
144172
def data(self):

nibabel/gifti/gifti.py

+33-13
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,44 @@ class GiftiMetaData(CaretMetaData):
3030
""" A sequence of GiftiNVPairs containing metadata for a gifti data array
3131
"""
3232

33-
def __init__(self, *args, **kwargs):
33+
@staticmethod
34+
def _sanitize(args, kwargs):
35+
""" Sanitize and warn on deprecated arguments
36+
37+
Accept nvpair positional/keyword argument that is a single
38+
``GiftiNVPairs`` object.
39+
40+
>>> import pytest
41+
>>> GiftiMetaData._sanitize((), {})
42+
((), {})
43+
>>> GiftiMetaData._sanitize(([("key", "val")],), {})
44+
(([('key', 'val')],), {})
45+
>>> GiftiMetaData._sanitize((), {"key": "val"})
46+
((), {'key': 'val'})
47+
>>> nvpairs = GiftiNVPairs(name='key', value='val')
48+
>>> with pytest.warns(FutureWarning):
49+
... GiftiMetaData._sanitize((nvpairs,), {})
50+
((), {'key': 'val'})
51+
>>> with pytest.warns(FutureWarning):
52+
... GiftiMetaData._sanitize((), {'nvpair': nvpairs})
53+
((), {'key': 'val'})
54+
"""
3455
dep_init = False
3556
# Positional arg
3657
dep_init |= not kwargs and len(args) == 1 and isinstance(args[0], GiftiNVPairs)
3758
# Keyword arg
3859
dep_init |= not args and list(kwargs) == ["nvpair"]
39-
if dep_init:
40-
warnings.warn(
41-
"GiftiMetaData now has a dict-like interface. "
42-
"See ``pydoc dict`` for initialization options. "
43-
"Passing ``GiftiNVPairs()`` or using the ``nvpair`` "
44-
"keyword will fail or behave unexpectedly in NiBabel 6.0.",
45-
FutureWarning, stacklevel=2)
46-
super().__init__()
47-
pair = args[0] if args else kwargs.get("nvpair")
48-
self[pair.name] = pair.value
49-
else:
50-
super().__init__(*args, **kwargs)
60+
if not dep_init:
61+
return args, kwargs
62+
63+
warnings.warn(
64+
"GiftiMetaData now has a dict-like interface. "
65+
"See ``pydoc dict`` for initialization options. "
66+
"Passing ``GiftiNVPairs()`` or using the ``nvpair`` "
67+
"keyword will fail or behave unexpectedly in NiBabel 6.0.",
68+
FutureWarning, stacklevel=3)
69+
pair = args[0] if args else kwargs.get("nvpair")
70+
return (), {pair.name: pair.value}
5171

5272
@property
5373
def data(self):

0 commit comments

Comments
 (0)