def warns(expected_warning, *args, **kwargs):
__tracebackhide__ = True<EOL>if not args:<EOL><INDENT>match_expr = kwargs.pop("<STR_LIT>", None)<EOL>if kwargs:<EOL><INDENT>warnings.warn(<EOL>PYTEST_WARNS_UNKNOWN_KWARGS.format(args=sorted(kwargs)), stacklevel=<NUM_LIT:2><EOL>)<EOL><DEDENT>return WarningsChecker(expected_warning, match_expr=match_expr)<EOL><DEDENT>elif isinstance(args[<NUM_LIT:0>], str):<EOL><INDENT>warnings.warn(WARNS_EXEC, stacklevel=<NUM_LIT:2>)<EOL>code, = args<EOL>assert isinstance(code, str)<EOL>frame = sys._getframe(<NUM_LIT:1>)<EOL>loc = frame.f_locals.copy()<EOL>loc.update(kwargs)<EOL>with WarningsChecker(expected_warning):<EOL><INDENT>code = _pytest._code.Source(code).compile()<EOL>six.exec_(code, frame.f_globals, loc)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>func = args[<NUM_LIT:0>]<EOL>with WarningsChecker(expected_warning):<EOL><INDENT>return func(*args[<NUM_LIT:1>:], **kwargs)<EOL><DEDENT><DEDENT>
r"""Assert that code raises a particular class of warning.

    Specifically, the parameter ``expected_warning`` can be a warning class or
    sequence of warning classes, and the inside the ``with`` block must issue a warning of that class or
    classes.

    This helper produces a list of :class:`warnings.WarningMessage` objects,
    one for each warning raised.

    This function can be used as a context manager, or any of the other ways
    ``pytest.raises`` can be used::

        >>> with warns(RuntimeWarning):
        ...    warnings.warn("my warning", RuntimeWarning)

    In the context manager form you may use the keyword argument ``match`` to assert
    that the exception matches a text or regex::

        >>> with warns(UserWarning, match='must be 0 or None'):
        ...     warnings.warn("value must be 0 or None", UserWarning)

        >>> with warns(UserWarning, match=r'must be \d+$'):
        ...     warnings.warn("value must be 42", UserWarning)

        >>> with warns(UserWarning, match=r'must be \d+$'):
        ...     warnings.warn("this is not here", UserWarning)
        Traceback (most recent call last):
          ...
        Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted...