code: def _insert_doc_at_pattern(
      obj,
      *,
      message: str,
      pattern: str,
      insert_after: bool = True,
      directive: Optional[str] = None,
      skip_matches: int = 0,
  ) -> str:
      if "\n" in message:
          raise ValueError(
              "message shouldn't contain any newlines, since this function will insert "
              f"its own linebreaks when text wrapping: {message}"
          )
  
      doc = obj.__doc__.strip()
      if not doc:
          doc = ""
  
      if pattern == "" and insert_after:
          # Empty pattern + insert_after means that we want to append the message to the
          # end of the docstring.
          head = doc
          tail = ""
      else:
          tail = doc
          i = tail.find(pattern)
          skip_matches_left = skip_matches
          while i != -1:
              if insert_after:
                  # Set offset to the first character after the pattern.
                  offset = i + len(pattern)
              else:
                  # Set offset to the first character in the matched line.
                  offset = tail[:i].rfind("\n") + 1
              head = tail[:offset]
              tail = tail[offset:]
              skip_matches_left -= 1
              if skip_matches_left <= 0:
                  break
              elif not insert_after:
                  # Move past the found pattern, since we're skipping it.
                  tail = tail[i - offset + len(pattern) :]
              i = tail.find(pattern)
          else:
              raise ValueError(
                  f"Pattern {pattern} not found after {skip_matches} skips in docstring "
                  f"{doc}"
              )
      # Get indentation of the to-be-inserted text.
      after_lines = list(filter(bool, tail.splitlines()))
      if len(after_lines) > 0:
          lines = after_lines
      else:
          lines = list(filter(bool, reversed(head.splitlines())))
      # Should always have at least one non-empty line in the docstring.
      assert len(lines) > 0
      indent = " " * (len(lines[0]) - len(lines[0].lstrip()))
      # Handle directive.
      message = message.strip("\n")
      if directive is not None:
          base = f"{indent}.. {directive}::\n"
          message = message.replace("\n", "\n" + indent + " " * 4)
          message = base + indent + " " * 4 + message
      else:
          message = indent + message.replace("\n", "\n" + indent)
      # Add two blank lines before/after message, if necessary.
      if insert_after ^ (pattern == "\n\n"):
          # Only two blank lines before message if:
          # 1. Inserting message after pattern and pattern is not two blank lines.
          # 2. Inserting message before pattern and pattern is two blank lines.
          message = "\n\n" + message
      if (not insert_after) ^ (pattern == "\n\n"):
          # Only two blank lines after message if:
          # 1. Inserting message before pattern and pattern is not two blank lines.
          # 2. Inserting message after pattern and pattern is two blank lines.
          message = message + "\n\n"
  
      # Insert message before/after pattern.
      parts = [head, message, tail]
      # Build new docstring.
      obj.__doc__ = "".join(parts)
  
imports: import importlib
  import logging
  import os
  import pathlib
  import random
  import sys
  import threading
  import time
  import urllib.parse
  from collections import deque
  from types import ModuleType
  from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, Iterator, List, Optional, Tuple, TypeVar, Union
  import numpy
  import ray
  from ray._private.utils import _get_pyarrow_version
  from ray.data._internal.arrow_ops.transform_pyarrow import unify_schemas
  from ray.data.context import WARN_PREFIX, DataContext
label_desc: Use image node_id 12 for calling a global function with example usage: _insert_doc_at_pattern(obj) without return types