Skip to content

Commit

Permalink
Fix errors loading settings when profiles file can't be read (#15602)
Browse files Browse the repository at this point in the history
  • Loading branch information
desertaxle authored Oct 8, 2024
1 parent ca98922 commit 6d084cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/prefect/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,16 @@ def __init__(self, settings_cls: Type[BaseSettings]):
def _load_profile_settings(self) -> Dict[str, Any]:
"""Helper method to load the profile settings from the profiles.toml file"""

all_profile_data = toml.load(self.profiles_path)
if not self.profiles_path.exists():
return {}

try:
all_profile_data = toml.load(self.profiles_path)
except toml.TomlDecodeError:
warnings.warn(
f"Failed to load profiles from {self.profiles_path}. Please ensure the file is valid TOML."
)
return {}

if (
sys.argv[0].endswith("/prefect")
Expand Down
17 changes: 17 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ def test_include_secrets(self):
== "test"
)

def test_loads_when_profile_path_does_not_exist(self, monkeypatch):
monkeypatch.setenv("PREFECT_PROFILES_PATH", str(Path.home() / "nonexistent"))
monkeypatch.delenv("PREFECT_TEST_MODE", raising=False)
monkeypatch.delenv("PREFECT_UNIT_TEST_MODE", raising=False)
assert Settings().test_setting == "FOO"

def test_loads_when_profile_path_is_not_a_toml_file(self, monkeypatch, tmp_path):
monkeypatch.setenv("PREFECT_PROFILES_PATH", str(tmp_path / "profiles.toml"))
monkeypatch.delenv("PREFECT_TEST_MODE", raising=False)
monkeypatch.delenv("PREFECT_UNIT_TEST_MODE", raising=False)

with open(tmp_path / "profiles.toml", "w") as f:
f.write("Ceci n'est pas un fichier toml")

with pytest.warns(UserWarning, match="Failed to load profiles from"):
assert Settings().test_setting == "FOO"


class TestSettingAccess:
def test_get_value_root_setting(self):
Expand Down

0 comments on commit 6d084cc

Please sign in to comment.