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


def _get_typed_signature(fn: Callable[..., Any]) -> inspect.Signature:
  """Returns the signature of a callable with type annotations resolved.

  If postponed evaluation of type annotations (PEP 563) is enabled (e.g. via
  `from __future__ import annotations` in Python >= 3.7) then we will need to
  resolve the annotations from their string forms in order to access the real
  types within the signature.
  https://www.python.org/dev/peps/pep-0563/#resolving-type-hints-at-runtime

  Args:
    fn: A callable to get the signature of.

  Returns:
    An instance of `inspect.Signature`.
  """
  type_hints = typing.get_type_hints(fn) or {}
  orig_signature = inspect.signature(fn)
  new_params = []
  for key, orig_param in orig_signature.parameters.items():
    new_params.append(
        inspect.Parameter(
            name=key,
            default=orig_param.default,
            annotation=type_hints.get(key, orig_param.annotation),
            kind=orig_param.kind,
        ))
  return orig_signature.replace(parameters=new_params)