code: def expected_warnings(matching):
      r"""Context for use in testing to catch known warnings matching regexes
  
      Parameters
      ----------
      matching : list of strings or compiled regexes
          Regexes for the desired warning to catch
  
      Examples
      --------
      >>> from skimage import data, img_as_ubyte, img_as_float  # doctest: +SKIP
      >>> with expected_warnings(["precision loss"]):           # doctest: +SKIP
      ...     d = img_as_ubyte(img_as_float(data.coins()))      # doctest: +SKIP
  
      Notes
      -----
      Uses `all_warnings` to ensure all warnings are raised.
      Upon exiting, it checks the recorded warnings for the desired matching
      pattern(s).
      Raises a ValueError if any match was not found or an unexpected
      warning was raised.
      Allows for three types of behaviors: "and", "or", and "optional" matches.
      This is done to accommodate different build environments or loop conditions
      that may produce different warnings.  The behaviors can be combined.
      If you pass multiple patterns, you get an orderless "and", where all of the
      warnings must be raised.
      If you use the "|" operator in a pattern, you can catch one of several warnings.
      Finally, you can use "|\A\Z" in a pattern to signify it as optional.
      """
      with all_warnings() as w:
          # enter context
          yield w
          # exited user context, check the recorded warnings
          remaining = [
              m for m in matching if r"\A\Z" not in m.split("|")
          ]
          for warn in w:
              found = False
              for match in matching:
                  if re.search(match, str(warn.message)) is not None:
                      found = True
                      if match in remaining:
                          remaining.remove(match)
              if not found:
                  raise ValueError(
                      "Unexpected warning: %s" % str(warn.message)
                  )
          if len(remaining) > 0:
              msg = "No warning raised matching:\n%s" % "\n".join(
                  remaining
              )
              raise ValueError(msg)
  
file_path: catboost/contrib/python/traitlets/py3/tests/_warnings.py
imports: import inspect
  import os
  import re
  import sys
  import warnings
  from contextlib import contextmanager
  from unittest import mock
label_desc: Use image node_id 2 for calling a global function with example usage: expected_warnings(matching) without return types