code: def pretty_untokenize(typed_tokens):
      text = []
      prev_was_space_delim = False
      prev_wants_space = False
      prev_was_open_paren_or_comma = False
      prev_was_object_like = False
      brackets = []
      for token_type, token in typed_tokens:
          assert token_type not in (
              tokenize.INDENT,
              tokenize.DEDENT,
              tokenize.NL,
          )
          if token_type == tokenize.NEWLINE:
              continue
          if token_type == tokenize.ENDMARKER:
              continue
          if token_type in (
              tokenize.NAME,
              tokenize.NUMBER,
              tokenize.STRING,
          ):
              if prev_wants_space or prev_was_space_delim:
                  text.append(" ")
              text.append(token)
              prev_wants_space = False
              prev_was_space_delim = True
          else:
              if token in ("(", "[", "{"):
                  brackets.append(token)
              elif brackets and token in (")", "]", "}"):
                  brackets.pop()
              this_wants_space_before = token in _python_space_before
              this_wants_space_after = token in _python_space_after
              # Special case for slice syntax: foo[:10]
              # Otherwise ":" is spaced after, like: "{1: ...}", "if a: ..."
              if token == ":" and brackets and brackets[-1] == "[":
                  this_wants_space_after = False
              # Special case for foo(*args), foo(a, *args):
              if token in ("*", "**") and prev_was_open_paren_or_comma:
                  this_wants_space_before = False
                  this_wants_space_after = False
              # Special case for "a = foo(b=1)":
              if token == "=" and not brackets:
                  this_wants_space_before = True
                  this_wants_space_after = True
              # Special case for unary -, +. Our heuristic is that if we see the
              # + or - after something that looks like an object (a NAME,
              # NUMBER, STRING, or close paren) then it is probably binary,
              # otherwise it is probably unary.
              if token in ("+", "-") and not prev_was_object_like:
                  this_wants_space_before = False
                  this_wants_space_after = False
              if prev_wants_space or this_wants_space_before:
                  text.append(" ")
              text.append(token)
              prev_wants_space = this_wants_space_after
              prev_was_space_delim = False
          if (
              token_type
              in (tokenize.NAME, tokenize.NUMBER, tokenize.STRING)
              or token == ")"
          ):
              prev_was_object_like = True
          else:
              prev_was_object_like = False
          prev_was_open_paren_or_comma = token in ("(", ",")
      return "".join(text)
  
file_path: patsy/patsy/tokens.py
imports: import tokenize
  from six.moves import cStringIO
  from patsy import PatsyError
  from patsy.origin import Origin
label_desc: Use image node_id 3 for calling a global function with example usage: pretty_untokenize(typed_tokens) and returns: str