import ast
import yaml


def parse_ast_doc(node):
    # type: (ast.Str) -> dict
    """Parses documentation from python AST string node.

    :param ast.Str node: An AST string node.
    :rtype: dict
    :return: Documentation dictionary.
    :raises ValueError: if cannot parse documentation
            (e.g. documentation is not a valid yaml dictionary)
    """
    if not isinstance(node.value, ast.Str):
        raise ValueError('Invalid field type, string expected')

    try:
        documentation = yaml.safe_load(node.value.s)
    except yaml.YAMLError as e:
        raise ValueError(e)

    if not isinstance(documentation, dict):
        raise ValueError('Invalid YAML document, dictionary expected')
    return documentation