Repository: microsoft/graphrag
File: graphrag/config/load_config.py (Python)

Code before:
    Parameters
    ----------
    root_dir : str | Path
        The root directory of the project. Will search for the config file in this directory.
    config_filepath : str | Path | None
        The path to the config file.
        If None, searches for config file in root and
        if not found creates a default configuration.
    run_id : str | None
        The run id to use for resolving timestamp_paths.
    """
    root = Path(root_dir).resolve()

    # If user specified a config file path then it is required
    if config_filepath is not None:
        config_path = (root / config_filepath).resolve()
        if not config_path.exists():
            msg = f"Specified Config file not found: {config_path}"
            raise FileNotFoundError(msg)

    # Else optional resolve the config path from the root directory if it exists
    try:
        config_path = resolve_config_path_with_root(root)
        config = load_config_from_file(config_path)
    except FileNotFoundError:
        # If config file not found in root directory create default configuration
        config = create_graphrag_config(root_dir=str(root))

    if run_id is not None:
        config.storage.base_dir = str(
            resolve_timestamp_path((root / config.storage.base_dir).resolve(), run_id)
        )
        config.reporting.base_dir = str(
            resolve_timestamp_path((root / config.reporting.base_dir).resolve(), run_id)
        )
    else:
        config.storage.base_dir = str(
            resolve_timestamp_path((root / config.storage.base_dir).resolve())
        )
        config.reporting.base_dir = str(
            resolve_timestamp_path((root / config.reporting.base_dir).resolve())
        )

    return config


Review comment (suggestion):
Could we change to Path in the signature? Wonder if we can reduce our faulty approach of supporting that many types in method signatures :)

Code after:
    Parameters
    ----------
    root_dir : str | Path
        The root directory of the project. Will search for the config file in this directory.
    config_filepath : str | Path | None
        The path to the config file.
        If None, searches for config file in root and
        if not found creates a default configuration.
    run_id : str | None
        The run id to use for resolving timestamp_paths.
    """
    root = Path(root_dir).resolve()

    # If user specified a config file path then it is required
    if config_filepath:
        config_path = (root / config_filepath).resolve()
        if not config_path.exists():
            msg = f"Specified Config file not found: {config_path}"
            raise FileNotFoundError(msg)

    # Else optional resolve the config path from the root directory if it exists
    config_path = search_for_config_in_root_dir(root)
    if config_path:
        config = load_config_from_file(config_path)
    else:
        config = create_graphrag_config(root_dir=str(root))

    if run_id:
        config.storage.base_dir = str(
            resolve_timestamp_path((root / config.storage.base_dir).resolve(), run_id)
        )
        config.reporting.base_dir = str(
            resolve_timestamp_path((root / config.reporting.base_dir).resolve(), run_id)
        )
    else:
        config.storage.base_dir = str(
            resolve_timestamp_path((root / config.storage.base_dir).resolve())
        )
        config.reporting.base_dir = str(
            resolve_timestamp_path((root / config.reporting.base_dir).resolve())
        )

    return config