import inspect


def kwargs_filter(func, kwargs: dict):  # [ElegantRL.2021.12.12]
    """How does one ignore `unexpected keyword arguments passed to a function`?
    https://stackoverflow.com/a/67713935/9293137

    class ClassTest:
        def __init__(self, a, b=1):
            print(f'| ClassTest: a + b == {a + b}')

    old_kwargs = {'a': 1, 'b': 2, 'c': 3}
    new_kwargs = kwargs_filter(ClassTest.__init__, old_kwargs)
    assert new_kwargs == {'a': 1, 'b': 2}
    test = ClassTest(**new_kwargs)

    :param func: func(**kwargs)
    :param kwargs: the KeyWordArguments wait for
    :return: kwargs: [dict] filtered kwargs
    """

    sign = inspect.signature(func).parameters.values()
    sign = set([val.name for val in sign])

    common_args = sign.intersection(kwargs.keys())
    return {key: kwargs[key] for key in common_args}