-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Catch specgram warnings in testing #7866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -1491,6 +1492,32 @@ def check_maxfreq(self, spec, fsp, fstims): | |||
del fstimst[-1] | |||
spect[maxind-5:maxind+5] = 0 | |||
|
|||
class catch_specgram_warnings(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can have one fewer level of nesting with the following standard trick:
def catch_specgram_warnings(n_specgram_calls, func=None):
if func is None:
return functools.partial(catch_specgram_warnings, n_specgram_calls)
@functools.wraps(func)
def wrapped(...): ...
return wrapped
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, so I now have
def catch_specgram_warnings(n_specgram_calls, func=None):
if func is None:
return functools.partial(catch_specgram_warnings, n_specgram_calls)
@functools.wraps(func)
def wrapped(self_obj):
# Decide how many warnings will be thrown
n_warnings = 0
if self_obj.NFFT_specgram is None:
if 256 >= len(self_obj.y):
n_warnings = 1
elif self_obj.NFFT_specgram >= len(self_obj.y):
n_warnings = 1
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
expected_warnings = self.n_specgram_calls * n_warnings
# Run test
test_function(self_obj)
assert len(w) == expected_warnings,\
'warnings:%s, expected:%s' % (len(w), expected_warnings)
return wrapped
but this is erroring with
======================================================================
ERROR: Failure: NameError (name 'catch_specgram_warnings' is not defined)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/dstansby/miniconda3/lib/python3.5/site-packages/nose/failure.py", line 39, in runTest
raise self.exc_val.with_traceback(self.tb)
File "/Users/dstansby/miniconda3/lib/python3.5/site-packages/nose/loader.py", line 407, in loadTestsFromName
module = resolve_name(addr.module)
File "/Users/dstansby/miniconda3/lib/python3.5/site-packages/nose/util.py", line 312, in resolve_name
module = __import__('.'.join(parts_copy))
File "/Users/dstansby/matplotlib/lib/matplotlib/tests/test_mlab.py", line 1313, in <module>
class Test_spectral_nosig_real_onesided(CleanupTestCase):
File "/Users/dstansby/matplotlib/lib/matplotlib/tests/test_mlab.py", line 1964, in Test_spectral_nosig_real_onesided
@catch_specgram_warnings(1)
File "/Users/dstansby/matplotlib/lib/matplotlib/tests/test_mlab.py", line 1497, in catch_specgram_warnings
return functools.partial(catch_specgram_warnings, n_specgram_calls)
NameError: name 'catch_specgram_warnings' is not defined
----------------------------------------------------------------------
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is because it is defined in a class, so it becomes a method of that class rather than a freestanding function. You need to hoist it out.
But I agree with @efiring that it'd be better to just try not triggering the warnings in the first place.
👍 thank you! I had meant to create an issue for this but it got lost. |
Would it be harder to handle this via supplying arguments that don't trigger the warning? |
I've raised an issue (#7967), would recommend supplying arguments that don't trigger the warning to all but one test, and then just use a simple warnings filter to test that one case. |
#7845 introduced lots of warnings when running tests. This gets rid of them in the easiest way I could think of, but is the first time I've done any decorating so there may be some room for improvement.