def at_children(self) -> "Selection":
    """Selects all direct children of each selected subtree.

    This can be used to implement recursive tree traversals in a generic way,
    using something like::

      def traverse(subtree):
        # ... process the subtree before recursive call ...
        subtree = select(subtree).at_children().apply(traverse)
        # ... process the subtree after the recursive call ...
        return subtree

      new_value = traverse(value)

    Returns:
      A new selection that selects every direct child of a selected subtree.
      If any leaves were previously selected, those leaves will no longer be
      selected (since they have no children).
    """

    def process(subtree):
      maybe_children = penzai_tree_util.tree_flatten_exactly_one_level(subtree)
      if maybe_children:
        keyed_children, treedef = maybe_children
        return treedef.unflatten(
            [_InProgressSelectionBoundary(child) for _, child in keyed_children]
        )
      else:
        return subtree

    with _wrap_selection_errors(self):
      return _build_selection_from_boundary(self.apply(process))
Selects all direct children of each selected subtree.

    This can be used to implement recursive tree traversals in a generic way,
    using something like::

      def traverse(subtree):
        # ... process the subtree before recursive call ...
        subtree = select(subtree).at_children().apply(traverse)
        # ... process the subtree after the recursive call ...
        return subtree

      new_value = traverse(value)

    Returns:
      A new selection that selects every direct child of a selected subtree.
      If any leaves were previously selected, those leaves will no longer be
      selected (since they have no children).
    
penzai/core/selectors.py