import inspect
from typing import Dict
from typing import Any
from typing import Callable


def filter_kwargs(kwargs: Dict[str, Any], method: Callable) -> Dict[str, Any]:
    """Removes incompatible keyword arguments. This ignores any **kwargs
    catchall in method signature, and only returns args specifically
    present as keyhwords in the method signature which are also not
    positional only.

    Args:
    -----
    params: dict
        Keyword arguments to pass to method.
    method: callable
        The method for which to check valid parameters.

    Returns:
    --------
    params: dict
        Filtered keyword arguments.
    """

    meth_params = inspect.signature(method).parameters
    kwargs = kwargs.copy()
    for key in set(kwargs.keys()):
        if (
            key not in meth_params
            or meth_params[key].kind == inspect.Parameter.POSITIONAL_ONLY
        ):
            del kwargs[key]
    return kwargs