Skip to content

treeo.Filter

Mixin that adds a .filter() method to the class.

filter(self, *filters, *, flatten_mode=None)

filter is a wrapper over treeo.filter that passes self as the first argument.

Parameters:

Name Type Description Default
*filters Union[Type[Any], Callable[[FieldInfo], bool]]

Types to filter by, membership is determined by issubclass, or callables that take in a FieldInfo and return a bool.

()
flatten_mode Union[treeo.tree.FlattenMode, str]

Sets a new FlattenMode context for the operation.

None

Returns:

Type Description
~A

A new pytree with the filtered fields.

Source code in treeo/mixins.py
def filter(
    self: A,
    *filters: api.Filter,
    flatten_mode: tp.Union[api.FlattenMode, str, None] = None,
) -> A:
    """
    `filter` is a wrapper over `treeo.filter` that passes `self` as the first argument.

    Arguments:
        *filters: Types to filter by, membership is determined by `issubclass`, or
            callables that take in a `FieldInfo` and return a `bool`.
        flatten_mode: Sets a new `FlattenMode` context for the operation.

    Returns:
        A new pytree with the filtered fields.
    """
    return api.filter(self, *filters, flatten_mode=flatten_mode)