def rebuild_optional(matched_group: str) -> str:
    """
    Rebuild `Union[T, None]` as `Optional[T]`.

    Arguments:
        matched_group: The matched group when matching against a regular expression (by the parent caller).

    Returns:
        The rebuilt type string.
    """
    brackets_level = 0
    for char in matched_group:
        if char == "," and brackets_level == 0:
            return f"Union[{matched_group}]"
        elif char == "[":
            brackets_level += 1
        elif char == "]":
            brackets_level -= 1
    return matched_group