def handling(
      cls,
      body: layer_base.LayerLike,
      tag: SideOutputTag | None = None,
      tag_predicate: Callable[[SideOutputTag], bool] | None = None,
      handler_id: str | None = None,
  ) -> CollectingSideOutputs:
    """Builds a ``CollectingSideOutputs`` that handles effects in this layer.

    Args:
      body: The layer to wrap. Usually will contain `SideOutputRequest` nodes.
      tag: A tag to collect. If None, defers to ``tag_predicate``.
      tag_predicate: A predicate to use to select which side outputs to collect.
        Should return True for tags to collect. If neither ``tag`` nor
        ``tag_predicate`` is specified, all side outputs are collected.
      handler_id: ID to use for the handler. If None, will be inferred.

    Returns:
      A ``CollectingSideOutputs`` handler wrapping ``body``, with its side
      output holes with the specified tasg replaced with references to this
      handler.
    """
    handler_id = effect_base.infer_or_check_handler_id(
        "WithSideInputFromArg", body, explicit_id=handler_id
    )

    if tag is None and tag_predicate is None:
      tag_predicate = lambda _: True
    elif tag is not None and tag_predicate is not None:
      raise ValueError(
          "Only one of `tag` and `tag_predicate` may be specified."
      )
    elif tag_predicate is None:
      tag_predicate = lambda t: t == tag

    def _make_ref(hole: SideOutputRequest):
      return HandledSideOutputRef(handler_id=handler_id, tag=hole.tag)

    handled_body = (
        selectors.select(body)
        .at_instances_of(SideOutputRequest)
        .where(lambda hole: tag_predicate(hole.tag))
        .apply(_make_ref)
    )
    return cls(handler_id=handler_id, body=handled_body)
Builds a ``CollectingSideOutputs`` that handles effects in this layer.

    Args:
      body: The layer to wrap. Usually will contain `SideOutputRequest` nodes.
      tag: A tag to collect. If None, defers to ``tag_predicate``.
      tag_predicate: A predicate to use to select which side outputs to collect.
        Should return True for tags to collect. If neither ``tag`` nor
        ``tag_predicate`` is specified, all side outputs are collected.
      handler_id: ID to use for the handler. If None, will be inferred.

    Returns:
      A ``CollectingSideOutputs`` handler wrapping ``body``, with its side
      output holes with the specified tasg replaced with references to this
      handler.
    
penzai/deprecated/v1/data_effects/side_output.py