from pathlib import Path
from typing import Tuple
from typing import Optional
import ast
from typing import cast


def get_docstring_and_version_via_ast(
    target: Path,
) -> Tuple[Optional[str], Optional[str]]:
    """
    This function is borrowed from flit's implementation, but does not attempt to import
    that file. If docstring or version can't be retrieved by this function,
    they are just left empty.
    """
    # read as bytes to enable custom encodings
    node = ast.parse(target.read_bytes())
    for child in node.body:
        # Only use the version from the given module if it's a simple
        # string assignment to __version__
        is_version_str = (
            isinstance(child, ast.Assign)
            and len(child.targets) == 1
            and isinstance(child.targets[0], ast.Name)
            and child.targets[0].id == "__version__"
            and isinstance(child.value, ast.Str)
        )
        if is_version_str:
            version: Optional[str] = cast(ast.Str, cast(ast.Assign, child).value).s
            break
    else:
        version = None
    return ast.get_docstring(node), version