def METHOD_NAME(
    contents: str, template_kwargs: Dict[str, Any]
) -> str:
    def lookup_var(match: Match) -> str:
        var_name = match.group(1)
        try:
            return template_kwargs[var_name]
        except KeyError:
            raise BadTemplateError(
                "Bad template, referenced template var that does not "
                "exist: '%s', for template contents:\n%s"
                % (var_name, contents)
            )
    new_contents = re.sub(VAR_REF_REGEX, lookup_var, contents)
    return new_contents