@contextmanager
def assert_produces_warning(expected_warning=Warning, filter_level=u'always', clear=None):
 with warnings.catch_warnings(record=True) as w:
  if (clear is not None):
   if (not _is_list_like(clear)):
    clear = [clear]
   for m in clear:
    getattr(m, u'__warningregistry__', {}).clear()
  saw_warning = False
  warnings.simplefilter(filter_level)
  (yield w)
  extra_warnings = []
  for actual_warning in w:
   if (expected_warning and issubclass(actual_warning.category, expected_warning)):
    saw_warning = True
   else:
    extra_warnings.append(actual_warning.category.__name__)
  if expected_warning:
   assert saw_warning, (u'Did not see expected warning of class %r.' % expected_warning.__name__)
  assert (not extra_warnings), (u'Caused unexpected warning(s): %r.' % extra_warnings)

@ contextmanager def assert_produces_warning ( expected_warning = Warning , filter_level = u'always' , clear = None ) : with warnings . catch_warnings ( record = True ) as w : if ( clear is not None ) : if ( not _is_list_like ( clear ) ) : clear = [ clear ] for m in clear : getattr ( m , u'__warningregistry__' , { } ) . clear ( ) saw_warning = False warnings . simplefilter ( filter_level ) ( yield w ) extra_warnings = [ ] for actual_warning in w : if ( expected_warning and issubclass ( actual_warning . category , expected_warning ) ) : saw_warning = True else : extra_warnings . append ( actual_warning . category . __name__ ) if expected_warning : assert saw_warning , ( u'Did not see expected warning of class %r.' % expected_warning . __name__ ) assert ( not extra_warnings ) , ( u'Caused unexpected warning(s): %r.' % extra_warnings )
context manager for running code that expects to raise warnings .