def maybe_parentheses(obj, operator: str = " ") -> str:
    """
    Wrap the string (or object's string representation) in parentheses, but only if required.
    :param obj:
        The object to (potentially) wrap up in parentheses. If it is not a string, it is converted
        to a string first.
    :param operator:
        The operator that needs precedence rules clarified. The default is ``' '`` (a space).
    :return: The (possibly) parenthesized string.
    """
    s = str(obj)
    if operator not in s:
        return s

    # if there are already wrapping punctuation marks, there may not be a need to add additional
    # ones
    open_mark = s[0]
    if open_mark in "([{":
        groupings = [s[0]]
        for c in s[1:-1]:
            if c in "([{":
                groupings.append(c)
            elif c == "}":
                if groupings[-1] == "{":
                    groupings.pop()
                else:
                    groupings.clear()
            elif c == "]":
                if groupings[-1] == "[":
                    groupings.pop()
                else:
                    groupings.clear()
            elif c == ")":
                if groupings[-1] == "(":
                    groupings.pop()
                else:
                    groupings.clear()

            if not groupings:
                # we balanced out all groupings (or just his a syntax error in unmatched groupings);
                # add clarifying parentheses
                return "(" + s + ")"
        if (
            (groupings[0] == "(" and groupings[-1] == ")")
            or (groupings[0] == "[" and groupings[-1] == "]")
            or (groupings[0] == "{" and groupings[-1] == "}")
        ):
            return s
    return "(" + s + ")" if operator in s else s