Skip to content

Commit

Permalink
Merge pull request materialsproject#522 from materialsproject/warn-on…
Browse files Browse the repository at this point in the history
…-empty-config

Warn on empty config
  • Loading branch information
janosh authored Sep 13, 2023
2 parents ff9dfa7 + 7d16877 commit ecf80af
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/atomate2/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Settings for atomate2."""

import warnings
from pathlib import Path
from typing import Literal, Optional, Tuple, Union

Expand Down Expand Up @@ -177,7 +178,17 @@ def load_default_settings(cls, values):

new_values = {}
if Path(config_file_path).expanduser().exists():
new_values.update(loadfn(Path(config_file_path).expanduser()))

new_values.update(values)
return new_values
if Path(config_file_path).stat().st_size == 0:
warnings.warn(
f"Using atomate2 config file at {config_file_path} but it's empty",
stacklevel=2,
)
else:
try:
new_values.update(loadfn(config_file_path))
except ValueError:
raise SyntaxError(
f"atomate2 config file at {config_file_path} is unparsable"
) from None

return {**new_values, **values}
26 changes: 26 additions & 0 deletions tests/common/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest


def test_empty_and_invalid_config_file(clean_dir):
import os
from pathlib import Path

from atomate2.settings import Atomate2Settings

# set path to load settings from though ATOMATE2_CONFIG_FILE env variable
config_file_path = Path.cwd() / "test-atomate2-config.yaml"
os.environ["ATOMATE2_CONFIG_FILE"] = str(config_file_path)

# test warning if config file is empty
config_file_path.touch()
with pytest.warns(
UserWarning, match="Using atomate2 config file at .+ but it's empty"
):
Atomate2Settings()

# test error if the file exists and contains invalid YAML
with open(config_file_path, "w") as file:
file.write("invalid yaml")

with pytest.raises(SyntaxError, match="atomate2 config file at"):
Atomate2Settings()

0 comments on commit ecf80af

Please sign in to comment.