Skip to content

Commit 2d64a6d

Browse files
author
Ben Cipollini
committed
RF: be more careful about warnings, test them!
1 parent 5495816 commit 2d64a6d

File tree

2 files changed

+68
-41
lines changed

2 files changed

+68
-41
lines changed

nipype/interfaces/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def _requires_warn(self, obj, name, old, new):
421421
if not msg:
422422
msg = 'Input %s requires inputs: %s' \
423423
% (name, ', '.join(trait_spec.requires))
424-
if msg:
424+
if msg: # only one requires warning at a time.
425425
warn(msg)
426426

427427
def _deprecated_warn(self, obj, name, old, new):
@@ -445,10 +445,11 @@ def _deprecated_warn(self, obj, name, old, new):
445445
if LooseVersion(str(trait_spec.deprecated)) < nipype_version:
446446
raise TraitError(msg)
447447
else:
448+
if trait_spec.new_name:
449+
msg += 'Unsetting old value %s; setting new value %s.' % (
450+
name, trait_spec.new_name)
448451
warn(msg)
449452
if trait_spec.new_name:
450-
warn('Unsetting %s and setting %s.' % (name,
451-
trait_spec.new_name))
452453
self.trait_set(trait_change_notify=False,
453454
**{'%s' % name: Undefined,
454455
'%s' % trait_spec.new_name: new})

nipype/interfaces/tests/test_base.py

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import tempfile
99
import shutil
10+
import warnings
1011

1112
from nipype.testing import (assert_equal, assert_not_equal, assert_raises,
1213
assert_true, assert_false, with_setup, package_check,
@@ -16,7 +17,8 @@
1617
from nipype.interfaces.base import Undefined, config
1718
from traits.testing.nose_tools import skip
1819
import traits.api as traits
19-
#test Bunch
20+
21+
2022
def test_bunch():
2123
b = nib.Bunch()
2224
yield assert_equal, b.__dict__,{}
@@ -138,43 +140,67 @@ class MyInterface(nib.BaseInterface):
138140
yield assert_equal, myif.inputs.kung, 2.0
139141

140142
def test_deprecation():
141-
class DeprecationSpec1(nib.TraitedSpec):
142-
foo = nib.traits.Int(deprecated='0.1')
143-
spec_instance = DeprecationSpec1()
144-
set_foo = lambda : setattr(spec_instance, 'foo', 1)
145-
yield assert_raises, nib.TraitError, set_foo
146-
class DeprecationSpec1numeric(nib.TraitedSpec):
147-
foo = nib.traits.Int(deprecated='0.1')
148-
spec_instance = DeprecationSpec1numeric()
149-
set_foo = lambda : setattr(spec_instance, 'foo', 1)
150-
yield assert_raises, nib.TraitError, set_foo
151-
class DeprecationSpec2(nib.TraitedSpec):
152-
foo = nib.traits.Int(deprecated='100', new_name='bar')
153-
spec_instance = DeprecationSpec2()
154-
set_foo = lambda : setattr(spec_instance, 'foo', 1)
155-
yield assert_raises, nib.TraitError, set_foo
156-
class DeprecationSpec3(nib.TraitedSpec):
157-
foo = nib.traits.Int(deprecated='1000', new_name='bar')
158-
bar = nib.traits.Int()
159-
spec_instance = DeprecationSpec3()
160-
not_raised = True
161-
try:
162-
spec_instance.foo = 1
163-
except nib.TraitError:
164-
not_raised = False
165-
yield assert_true, not_raised
166-
class DeprecationSpec3(nib.TraitedSpec):
167-
foo = nib.traits.Int(deprecated='1000', new_name='bar')
168-
bar = nib.traits.Int()
169-
spec_instance = DeprecationSpec3()
170-
not_raised = True
171-
try:
172-
spec_instance.foo = 1
173-
except nib.TraitError:
174-
not_raised = False
175-
yield assert_true, not_raised
176-
yield assert_equal, spec_instance.foo, Undefined
177-
yield assert_equal, spec_instance.bar, 1
143+
with warnings.catch_warnings(record=True) as w:
144+
warnings.filterwarnings('always', '', UserWarning)
145+
146+
class DeprecationSpec1(nib.TraitedSpec):
147+
foo = nib.traits.Int(deprecated='0.1')
148+
spec_instance = DeprecationSpec1()
149+
set_foo = lambda : setattr(spec_instance, 'foo', 1)
150+
yield assert_raises, nib.TraitError, set_foo
151+
yield assert_equal, len(w), 0, 'no warnings, just errors'
152+
153+
with warnings.catch_warnings(record=True) as w:
154+
warnings.filterwarnings('always', '', UserWarning)
155+
156+
class DeprecationSpec1numeric(nib.TraitedSpec):
157+
foo = nib.traits.Int(deprecated='0.1')
158+
spec_instance = DeprecationSpec1numeric()
159+
set_foo = lambda : setattr(spec_instance, 'foo', 1)
160+
yield assert_raises, nib.TraitError, set_foo
161+
yield assert_equal, len(w), 0, 'no warnings, just errors'
162+
163+
with warnings.catch_warnings(record=True) as w:
164+
warnings.filterwarnings('always', '', UserWarning)
165+
166+
class DeprecationSpec2(nib.TraitedSpec):
167+
foo = nib.traits.Int(deprecated='100', new_name='bar')
168+
spec_instance = DeprecationSpec2()
169+
set_foo = lambda : setattr(spec_instance, 'foo', 1)
170+
yield assert_raises, nib.TraitError, set_foo
171+
yield assert_equal, len(w), 0, 'no warnings, just errors'
172+
173+
with warnings.catch_warnings(record=True) as w:
174+
warnings.filterwarnings('always', '', UserWarning)
175+
176+
class DeprecationSpec3(nib.TraitedSpec):
177+
foo = nib.traits.Int(deprecated='1000', new_name='bar')
178+
bar = nib.traits.Int()
179+
spec_instance = DeprecationSpec3()
180+
not_raised = True
181+
try:
182+
spec_instance.foo = 1
183+
except nib.TraitError:
184+
not_raised = False
185+
yield assert_true, not_raised
186+
yield assert_equal, len(w), 1, 'deprecated warning 1 %s' % [w1.message for w1 in w]
187+
188+
with warnings.catch_warnings(record=True) as w:
189+
warnings.filterwarnings('always', '', UserWarning)
190+
191+
class DeprecationSpec3(nib.TraitedSpec):
192+
foo = nib.traits.Int(deprecated='1000', new_name='bar')
193+
bar = nib.traits.Int()
194+
spec_instance = DeprecationSpec3()
195+
not_raised = True
196+
try:
197+
spec_instance.foo = 1
198+
except nib.TraitError:
199+
not_raised = False
200+
yield assert_true, not_raised
201+
yield assert_equal, spec_instance.foo, Undefined
202+
yield assert_equal, spec_instance.bar, 1
203+
yield assert_equal, len(w), 1, 'deprecated warning 2 %s' % [w1.message for w1 in w]
178204

179205

180206
def test_namesource():

0 commit comments

Comments
 (0)