Skip to content

filter_by_dict

faststream.utils.data.filter_by_dict #

filter_by_dict(
    typed_dict: Type[TypedDictCls], data: AnyDict
) -> TypedDictCls

Filter a dictionary based on a typed dictionary.

PARAMETER DESCRIPTION
typed_dict

The typed dictionary to filter by.

TYPE: Type[TypedDictCls]

data

The dictionary to filter.

TYPE: AnyDict

RETURNS DESCRIPTION
TypedDictCls

A new instance of the typed dictionary with only the keys present in the data dictionary.

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Source code in faststream/utils/data.py
def filter_by_dict(typed_dict: Type[TypedDictCls], data: AnyDict) -> TypedDictCls:
    """Filter a dictionary based on a typed dictionary.

    Args:
        typed_dict: The typed dictionary to filter by.
        data: The dictionary to filter.

    Returns:
        A new instance of the typed dictionary with only the keys present in the data dictionary.
    !!! note

        The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
    """
    annotations = typed_dict.__annotations__
    return typed_dict(  # type: ignore
        {k: v for k, v in data.items() if k in annotations}
    )

Last update: 2023-11-13