def get_docstring_and_version_via_ast(
    target: Path,
) -> tuple[str | None, str | None]:
    """
    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
    if not target.exists():
        return None, None
    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.Constant)
            and isinstance(child.value.value, str)
        )
        if is_version_str:
            version: str | None = cast(ast.Constant, cast(ast.Assign, child).value).value
            break
    else:
        version = None
    return ast.get_docstring(node), version

    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.
    
src/pdm/formats/flit.py