Skip to content

Commit 6bc86df

Browse files
committed
RF: Update Gifti/Cifti2MetaData checks to _sanitize, add doctests
1 parent b18147b commit 6bc86df

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-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()
134+
<Cifti2MetaData {}>
135+
>>> Cifti2MetaData([("key", "val")])
136+
<Cifti2MetaData {'key': 'val'}>
137+
>>> Cifti2MetaData(key="val")
138+
<Cifti2MetaData {'key': 'val'}>
139+
>>> with pytest.warns(FutureWarning):
140+
... Cifti2MetaData(None)
141+
<Cifti2MetaData {}>
142+
>>> with pytest.warns(FutureWarning):
143+
... Cifti2MetaData(metadata=None)
144+
<Cifti2MetaData {}>
145+
>>> with pytest.warns(FutureWarning):
146+
... Cifti2MetaData(metadata={'key': 'val'})
147+
<Cifti2MetaData {'key': 'val'}>
148+
149+
Note that "metadata" could be a valid key:
150+
151+
>>> Cifti2MetaData(metadata='val')
152+
<Cifti2MetaData {'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

+35-13
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,46 @@ 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()
42+
<GiftiMetaData {}>
43+
>>> GiftiMetaData([("key", "val")])
44+
<GiftiMetaData {'key': 'val'}>
45+
>>> GiftiMetaData(key="val")
46+
<GiftiMetaData {'key': 'val'}>
47+
>>> GiftiMetaData({"key": "val"})
48+
<GiftiMetaData {'key': 'val'}>
49+
>>> nvpairs = GiftiNVPairs(name='key', value='val')
50+
>>> with pytest.warns(FutureWarning):
51+
... GiftiMetaData(nvpairs)
52+
<GiftiMetaData {'key': 'val'}>
53+
>>> with pytest.warns(FutureWarning):
54+
... GiftiMetaData(nvpair=nvpairs)
55+
<GiftiMetaData {'key': 'val'}>
56+
"""
3457
dep_init = False
3558
# Positional arg
3659
dep_init |= not kwargs and len(args) == 1 and isinstance(args[0], GiftiNVPairs)
3760
# Keyword arg
3861
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)
62+
if not dep_init:
63+
return args, kwargs
64+
65+
warnings.warn(
66+
"GiftiMetaData now has a dict-like interface. "
67+
"See ``pydoc dict`` for initialization options. "
68+
"Passing ``GiftiNVPairs()`` or using the ``nvpair`` "
69+
"keyword will fail or behave unexpectedly in NiBabel 6.0.",
70+
FutureWarning, stacklevel=3)
71+
pair = args[0] if args else kwargs.get("nvpair")
72+
return (), {pair.name: pair.value}
5173

5274
@property
5375
def data(self):

0 commit comments

Comments
 (0)