def wildcard_key_approx()

in chz/blueprint/_wildcard.py [0:0]


def wildcard_key_approx(key: str, target_str: str) -> tuple[float, str]:
    """
    Returns a score and a string representing what the key should have been to match the target.

    Currently only used in error messages.
    """
    if key.endswith("..."):
        raise ValueError("Wildcard not allowed at end of key")
    pattern = ["..."] if key.startswith("...") else []
    pattern += [x for x in re.split(r"(\.\.\.)|\.", key.removeprefix("...")) if x is not None]
    target = target_str.split(".")

    import difflib

    _grid: dict[tuple[int, int], tuple[float, tuple[str, ...]]] = {}

    def _match(i, j) -> tuple[float, tuple[str, ...]]:
        if i == len(pattern):
            return (1, ()) if j == len(target) else (0, ())
        if j == len(target):
            return (0, ())
        if (i, j) in _grid:
            return _grid[i, j]
        if pattern[i] == "...":
            with_wildcard = _match(i, j + 1)
            without_wildcard = _match(i + 1, j)
            if with_wildcard[0] * _FUZZY_SIMILARITY > without_wildcard[0]:
                score, value = with_wildcard
                score *= _FUZZY_SIMILARITY
            else:
                score, value = without_wildcard
            if value and value[0] != "...":
                value = ("...",) + value
            ret = (score, value)
            _grid[i, j] = ret
            return ret

        ratio = difflib.SequenceMatcher(a=pattern[i], b=target[j]).ratio()
        if ratio >= _FUZZY_SIMILARITY:
            score, value = _match(i + 1, j + 1)
            score *= ratio
            if value and value[0] != "...":
                value = (target[j] + ".",) + value
            else:
                value = (target[j],) + value
            ret = (score, value)
            _grid[i, j] = ret
            return ret
        return 0, ()

    score, value = _match(0, 0)
    return score, "".join(value)