import inspect


def has_arg(fn, name, accept_all=False):
    """Check if a callable accepts a given keyword argument.

    See https://github.com/tensorflow/tensorflow/pull/37004

    Arguments:
      fn: Callable to inspect.
      name: Check if `fn` can be called with `name` as a keyword argument.
      accept_all: What to return if there is no parameter called `name` but the
        function accepts a `**kwargs` argument.
    Returns:
      bool, whether `fn` accepts a `name` keyword argument.
    """
    arg_spec = inspect.getfullargspec(fn)
    if accept_all and arg_spec.varkw is not None:
        return True
    return name in arg_spec.args or name in arg_spec.kwonlyargs