(
    mapping: Mapping, destination_type: Type[T], instantiator: Callable = None, expand_kwargs=True
)
Convert a dictionary-like object into an instance of the given type

    This conversion is performed recursively. If the passed type
    class contains child structures, then the corresponding
    child keys of the dictionary will be mapped.

    If `instantiator` is provided, it will be called rather than simply
    calling `destination_type`.

    If `expand_kwargs` is True then the the parameters will be
    expended (i.e. **) when the `destination_type` / `instantiator`
    is called.
Convert a dictionary-like object into an instance of the given type
Convert a dictionary - like object into an instance of the given type
def _mapping_to_instance(
    mapping: Mapping, destination_type: Type[T], instantiator: Callable = None, expand_kwargs=True
) -> T:
    """Convert a dictionary-like object into an instance of the given type

    This conversion is performed recursively. If the passed type
    class contains child structures, then the corresponding
    child keys of the dictionary will be mapped.

    If `instantiator` is provided, it will be called rather than simply
    calling `destination_type`.

    If `expand_kwargs` is True then the the parameters will be
    expended (i.e. **) when the `destination_type` / `instantiator`
    is called.
    """
    import lightbus.config.structure

    hints = get_type_hints(destination_type, None, lightbus.config.structure.__dict__)
    parameters = {}

    if mapping is None:
        return None

    # Iterate through each key/type-hint pairing in the destination type
    for key, hint in hints.items():
        value = mapping.get(key)

        if key not in mapping:
            # This attribute has not been provided by in the mapping. Skip it
            continue

        hint_type, hint_args = parse_hint(hint)

        # Is this an Optional[] hint (which looks like Union[Thing, None])
        if is_optional(hint) and value is not None:
            # Yep, it's an Optional. So unwrap it.
            hint = hint_args[0]

        if issubclass_safe(hint_type, Mapping) and hint_args and len(hint_args) == 2:
            parameters[key] = dict()
            for k, v in value.items():
                parameters[key][k] = cast_to_hint(v, hint_args[1])
        else:
            parameters[key] = cast_to_hint(value, hint)

    instantiator = instantiator or destination_type
    if expand_kwargs:
        return instantiator(**parameters)
    else:
        return instantiator(parameters)
def _mapping_to_instance ( mapping : Mapping , destination_type : Type [ T ] , instantiator : Callable = None , expand_kwargs = True ) -> T : import lightbus . config . structure hints = get_type_hints ( destination_type , None , lightbus . config . structure . __dict__ ) parameters = { } if mapping is None : return None # Iterate through each key/type-hint pairing in the destination type for key , hint in hints . items ( ) : value = mapping . get ( key ) if key not in mapping : # This attribute has not been provided by in the mapping. Skip it continue hint_type , hint_args = parse_hint ( hint ) # Is this an Optional[] hint (which looks like Union[Thing, None]) if is_optional ( hint ) and value is not None : # Yep, it's an Optional. So unwrap it. hint = hint_args [ 0 ] if issubclass_safe ( hint_type , Mapping ) and hint_args and len ( hint_args ) == 2 : parameters [ key ] = dict ( ) for k , v in value . items ( ) : parameters [ key ] [ k ] = cast_to_hint ( v , hint_args [ 1 ] ) else : parameters [ key ] = cast_to_hint ( value , hint ) instantiator = instantiator or destination_type if expand_kwargs : return instantiator ( * * parameters ) else : return instantiator ( parameters )