@contextmanager<EOL>def assert_produces_warning(expected_warning=Warning, filter_level="<STR_LIT>",<EOL>clear=None, check_stacklevel=True,<EOL>raise_on_extra_warnings=True):
__tracebackhide__ = True<EOL>with warnings.catch_warnings(record=True) as w:<EOL><INDENT>if clear is not None:<EOL><INDENT>if not is_list_like(clear):<EOL><INDENT>clear = [clear]<EOL><DEDENT>for m in clear:<EOL><INDENT>try:<EOL><INDENT>m.__warningregistry__.clear()<EOL><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>saw_warning = False<EOL>warnings.simplefilter(filter_level)<EOL>yield w<EOL>extra_warnings = []<EOL>for actual_warning in w:<EOL><INDENT>if (expected_warning and issubclass(actual_warning.category,<EOL>expected_warning)):<EOL><INDENT>saw_warning = True<EOL>if check_stacklevel and issubclass(actual_warning.category,<EOL>(FutureWarning,<EOL>DeprecationWarning)):<EOL><INDENT>from inspect import getframeinfo, stack<EOL>caller = getframeinfo(stack()[<NUM_LIT:2>][<NUM_LIT:0>])<EOL>msg = ("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>).format(actual=actual_warning.filename,<EOL>caller=caller.filename,<EOL>message=actual_warning.message)<EOL>assert actual_warning.filename == caller.filename, msg<EOL><DEDENT><DEDENT>else:<EOL><INDENT>extra_warnings.append((actual_warning.category.__name__,<EOL>actual_warning.message,<EOL>actual_warning.filename,<EOL>actual_warning.lineno))<EOL><DEDENT><DEDENT>if expected_warning:<EOL><INDENT>msg = "<STR_LIT>".format(<EOL>name=expected_warning.__name__)<EOL>assert saw_warning, msg<EOL><DEDENT>if raise_on_extra_warnings and extra_warnings:<EOL><INDENT>raise AssertionError(<EOL>"<STR_LIT>".format(extra_warnings)<EOL>)<EOL><DEDENT><DEDENT>
Context manager for running code expected to either raise a specific
warning, or not raise any warnings. Verifies that the code raises the
expected warning, and that it does not raise any other unexpected
warnings. It is basically a wrapper around ``warnings.catch_warnings``.

Parameters
----------
expected_warning : {Warning, False, None}, default Warning
    The type of Exception raised. ``exception.Warning`` is the base
    class for all warnings. To check that no warning is returned,
    specify ``False`` or ``None``.
filter_level : str or None, default "always"
    Specifies whether warnings are ignored, displayed, or turned
    into errors.
    Valid values are:

    * "error" - turns matching warnings into exceptions
    * "ignore" - discard the warning
    * "always" - always emit a warning
    * "default" - print the warning the first time it is generated
      from each location
    * "module" - print the warning the first time it is generated
      from each module
    * "once" - print the warning the first time it is generated

clear : str, default None
    If not ``None`` then remove any previously raised warnings from
    the ``__warningsregistry__`` to ensure that no warning messages are
    suppressed by this context manager. If ``None`` is specified,
    the ``__warningsregistry__`` keeps track of which warnings have been
    shown, and does not show them again.
check_stacklevel : bool, default True
    If True, displays the line that called the function containing
    the warning to show were the function is called. Otherwise, the
    line that implements the function is displayed.
raise_on_extra_warnings : bool, default True
    Whether extra warnings not of the type `expected_warning` should
    cause the test to fail.

Examples
--------
>>> import warnings
>>> with assert_produces_warning():
...     warnings.warn(UserWarning())
...
>>> with assert_produces_warning(False):
...     warnings.warn(RuntimeWarning())
...
Traceback (most recent call last):
    ...
AssertionError: Caused unexpected warning(s): ['RuntimeWarning'].
>>> with assert_produces_warning(UserWarning):
...     warnings.warn(RuntimeWarning())
Traceback (most recent call last):
    ...
AssertionError: Did not see expected warning of class 'UserWarning'.

..warn:: This is *not* thread-safe.