docs: def collect_namespaces(values):
      """
      Gather mappings with keys of the shape '<base_key>__<sub_key>' as new dicts under '<base_key>', indexed by '<sub_key>'.
  
      >>> foo = dict(
      ...     foo__foo=1,
      ...     foo__bar=2,
      ...     bar__foo=3,
      ...     bar__bar=4,
      ...     foo_baz=5,
      ...     baz=6
      ... )
  
      >>> assert collect_namespaces(foo) == dict(
      ...     foo=dict(foo=1, bar=2),
      ...     bar=dict(foo=3, bar=4),
      ...     foo_baz=5,
      ...     baz=6
      ... )
  
      :type values: dict
      :rtype: dict
      """
      namespaces = {}
      result = dict(values)
      for key, value in values.items():
          parts = key.split('__', 1)  # pragma: no mutate
          if len(parts) == 2:
              prefix, name = parts
              if prefix not in namespaces:
                  initial_namespace = values.get(prefix)
                  if initial_namespace is None:
                      initial_namespace = {}
                  elif not isinstance(initial_namespace, dict):
                      initial_namespace = {initial_namespace: True}
                  namespaces[prefix] = initial_namespace
              namespaces[prefix][name] = result.pop(key)
      for prefix, namespace in namespaces.items():
          result[prefix] = namespace
      return result