Skip to content

Commit 8db5b25

Browse files
committed
RF: Change raise_exception to permissive; suppress warnings and exceptions with same mechanism
1 parent a29a041 commit 8db5b25

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

nipype/interfaces/base/core.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ def __init__(
183183
# Create input spec, disable any defaults that are unavailable due to
184184
# version, and then apply the inputs that were passed.
185185
self.inputs = self.input_spec()
186-
unavailable_traits = self._check_version_requirements(self.inputs, raise_exception=False)
186+
unavailable_traits = self._check_version_requirements(
187+
self.inputs, permissive=True
188+
)
187189
if unavailable_traits:
188190
self.inputs.trait_set(**{k: Undefined for k in unavailable_traits})
189191
self.inputs.trait_set(**inputs)
@@ -271,8 +273,12 @@ def _check_mandatory_inputs(self):
271273
):
272274
self._check_requires(spec, name, getattr(self.inputs, name))
273275

274-
def _check_version_requirements(self, trait_object, raise_exception=True):
276+
def _check_version_requirements(self, trait_object, permissive=False):
275277
""" Raises an exception on version mismatch
278+
279+
Set the ``permissive`` attribute to True to suppress warnings and exceptions.
280+
This is currently only used in __init__ to silently identify unavailable
281+
traits.
276282
"""
277283
unavailable_traits = []
278284
# check minimum version
@@ -290,15 +296,16 @@ def _check_version_requirements(self, trait_object, raise_exception=True):
290296
f"Nipype cannot validate the package version {version!r} for "
291297
f"{self.__class__.__name__}. Trait {name} requires version >={min_ver}."
292298
)
293-
iflogger.warning(f"{msg}. Please verify validity.")
299+
if not permissive:
300+
iflogger.warning(f"{msg}. Please verify validity.")
294301
if config.getboolean("execution", "stop_on_unknown_version"):
295302
raise ValueError(msg) from err
296303
continue
297304
if too_old:
298305
unavailable_traits.append(name)
299306
if not isdefined(getattr(trait_object, name)):
300307
continue
301-
if raise_exception:
308+
if not permissive:
302309
raise Exception(
303310
"Trait %s (%s) (version %s < required %s)"
304311
% (name, self.__class__.__name__, version, min_ver)
@@ -318,15 +325,16 @@ def _check_version_requirements(self, trait_object, raise_exception=True):
318325
f"Nipype cannot validate the package version {version!r} for "
319326
f"{self.__class__.__name__}. Trait {name} requires version <={max_ver}."
320327
)
321-
iflogger.warning(f"{msg}. Please verify validity.")
328+
if not permissive:
329+
iflogger.warning(f"{msg}. Please verify validity.")
322330
if config.getboolean("execution", "stop_on_unknown_version"):
323331
raise ValueError(msg) from err
324332
continue
325333
if too_new:
326334
unavailable_traits.append(name)
327335
if not isdefined(getattr(trait_object, name)):
328336
continue
329-
if raise_exception:
337+
if not permissive:
330338
raise Exception(
331339
"Trait %s (%s) (version %s > required %s)"
332340
% (name, self.__class__.__name__, version, max_ver)

nipype/interfaces/base/tests/test_core.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,12 @@ class input_spec(nib.TraitedSpec):
245245

246246
_version = "misparsed-garbage"
247247

248-
with caplog.at_level(logging.WARNING, logger="nipype.interface"):
249-
obj = DerivedInterface()
250-
assert len(caplog.records) == 2
248+
obj = DerivedInterface()
251249
obj.inputs.foo = 1
252250
obj.inputs.bar = 1
253251
with caplog.at_level(logging.WARNING, logger="nipype.interface"):
254252
obj._check_version_requirements(obj.inputs)
255-
assert len(caplog.records) == 4
253+
assert len(caplog.records) == 2
256254

257255

258256
def test_input_version_missing_error(caplog):
@@ -265,17 +263,15 @@ class input_spec(nib.TraitedSpec):
265263

266264
_version = "misparsed-garbage"
267265

268-
with caplog.at_level(logging.WARNING, logger="nipype.interface"):
269-
obj1 = DerivedInterface(foo=1)
270-
obj2 = DerivedInterface(bar=1)
271-
assert len(caplog.records) == 4
266+
obj1 = DerivedInterface(foo=1)
267+
obj2 = DerivedInterface(bar=1)
272268
with caplog.at_level(logging.WARNING, logger="nipype.interface"):
273269
with mock.patch.object(config, "getboolean", return_value=True):
274270
with pytest.raises(ValueError):
275271
obj1._check_version_requirements(obj1.inputs)
276272
with pytest.raises(ValueError):
277273
obj2._check_version_requirements(obj2.inputs)
278-
assert len(caplog.records) == 6
274+
assert len(caplog.records) == 2
279275

280276

281277
def test_unavailable_input():

0 commit comments

Comments
 (0)