import inspect


def _extract_method_kwargs(kwargs, method, remove=True):
    """Internal method to extract keyword arguments related to a given method.

    Parameters
    ----------
    kwargs : dict
        A dictionary of keyword arguments
    method : func
        A function with keyword arguments
    remove : bool
        Whether to remove the extracted keyword arguments from the original dict (the default is True)
    """
    method_params = inspect.signature(method).parameters
    method_kwargs = {}
    to_remove = []
    for key in kwargs:
        if key in method_params:
            method_kwargs[key] = kwargs[key]
            to_remove.append(key)
    if remove:
        for key in to_remove:
            del kwargs[key]
    return method_kwargs