from typing import Optional
from pathlib import Path
from typing import Dict
from typing import Any
import yaml
from typing import cast


def read_variables_from_yaml(yaml_file: Optional[Path] = None) -> Dict[str, Any]:
    """
    Reads a YAML file, that is expected to contain an entry 'variables'. Returns the dictionary for the 'variables'
    section of the file. If the file name is not given, an empty dictionary will be returned.
    :param yaml_file: The yaml file to read.
    :return: A dictionary with the variables from the yaml file.
    """
    if yaml_file is None:
        return dict()
    yaml_contents = yaml.load(yaml_file.open('r'), Loader=yaml.Loader)
    v = "variables"
    if v in yaml_contents:
        return cast(Dict[str, Any], yaml_contents[v])
    else:
        raise KeyError(f"The Yaml file was expected to contain a section '{v}'")