From 9ef8c074cbc9f60142062395c360e402e7da95a3 Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Tue, 12 Sep 2023 12:36:39 +0200 Subject: [PATCH 1/2] on existing but empty atomate config file: issue warning, not ValidationError 'NoneType' object is not iterable --- src/atomate2/settings.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/atomate2/settings.py b/src/atomate2/settings.py index f1af5a90d0..e75b128bd5 100644 --- a/src/atomate2/settings.py +++ b/src/atomate2/settings.py @@ -1,5 +1,6 @@ """Settings for atomate2.""" +import warnings from pathlib import Path from typing import Literal, Optional, Tuple, Union @@ -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} From 7d1687749e81d29c37ce2b84cbab2b6441f36bbd Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Tue, 12 Sep 2023 12:38:18 +0200 Subject: [PATCH 2/2] add test_empty_and_invalid_config_file --- tests/common/test_settings.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/common/test_settings.py diff --git a/tests/common/test_settings.py b/tests/common/test_settings.py new file mode 100644 index 0000000000..a80e195377 --- /dev/null +++ b/tests/common/test_settings.py @@ -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()