def attr_dict(cls: type, data: dict):
    """
    Removes keys from the passed dict ``data`` which don't exist on ``cls`` (thus would get rejected as kwargs),
    then create and return an instance of ``cls``, passing the filtered data as keyword args.

    Ensures that any keys in your dictionary which don't exist on ``cls`` are automatically filtered out, instead
    of causing an error due to unexpected keyword arguments.

    Example::

        >>> data = dict(timestamp="2019-01-01Z00:00", producer='eosio', block_num=1234, example='hello')
        >>> my_block = attr_dict(EOSBlock, data)


    :param cls:
    :param data:
    :return:
    """
    if hasattr(cls, '__attrs_attrs__'):
        cls_keys = [atr.name for atr in cls.__attrs_attrs__]
    else:
        cls_keys = [k for k in cls.__dict__.keys() if k[0] != '_']
    
    clean_data = {x: y for x, y in data.items() if x in cls_keys}
    return cls(**clean_data)