Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to default profile when profiles file is empty #16119

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/prefect/settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _load_profile_settings(self) -> Dict[str, Any]:
"""Helper method to load the profile settings from the profiles.toml file"""

if not self.profiles_path.exists():
return {}
return self._get_default_profile()

try:
all_profile_data = toml.load(self.profiles_path)
Expand All @@ -146,9 +146,16 @@ def _load_profile_settings(self) -> Dict[str, Any]:
profiles_data = all_profile_data.get("profiles", {})

if not active_profile or active_profile not in profiles_data:
return {}
return self._get_default_profile()
return profiles_data[active_profile]

def _get_default_profile(self) -> Dict[str, Any]:
"""Helper method to get the default profile"""
default_profile_data = toml.load(DEFAULT_PROFILES_PATH)
default_profile = default_profile_data.get("active", "ephemeral")
assert isinstance(default_profile, str)
return default_profile_data.get("profiles", {}).get(default_profile, {})

def get_field_value(
self, field: FieldInfo, field_name: str
) -> Tuple[Any, str, bool]:
Expand Down
65 changes: 65 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@ def test_loads_when_profile_path_does_not_exist(self, monkeypatch):
monkeypatch.delenv("PREFECT_TESTING_TEST_MODE", raising=False)
monkeypatch.delenv("PREFECT_TESTING_UNIT_TEST_MODE", raising=False)
assert Settings().testing.test_setting == "FOO"
# Should default to ephemeral profile
assert Settings().server.ephemeral.enabled

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"))
Expand Down Expand Up @@ -1444,6 +1446,69 @@ def test_environment_variables_take_precedence_over_toml_settings(
assert not Settings().server.ephemeral.enabled
assert not PREFECT_SERVER_ALLOW_EPHEMERAL_MODE.value()

def test_handle_profile_settings_without_active_profile(
self, monkeypatch, tmp_path
):
profiles_path = tmp_path / "profiles.toml"

monkeypatch.delenv("PREFECT_TESTING_TEST_MODE", raising=False)
monkeypatch.delenv("PREFECT_TESTING_UNIT_TEST_MODE", raising=False)
monkeypatch.setenv("PREFECT_PROFILES_PATH", str(profiles_path))

profiles_path.write_text(
textwrap.dedent(
"""
"""
)
)

# Should default to ephemeral profile
assert Settings().server.ephemeral.enabled

def test_handle_profile_settings_with_invalid_active_profile(
self, monkeypatch, tmp_path
):
profiles_path = tmp_path / "profiles.toml"

monkeypatch.delenv("PREFECT_TESTING_TEST_MODE", raising=False)
monkeypatch.delenv("PREFECT_TESTING_UNIT_TEST_MODE", raising=False)
monkeypatch.setenv("PREFECT_PROFILES_PATH", str(profiles_path))

profiles_path.write_text(
textwrap.dedent(
"""
active = "foo"

[profiles.bar]
PREFECT_LOGGING_LEVEL = "DEBUG"
"""
)
)

# Should default to ephemeral profile
assert Settings().server.ephemeral.enabled
assert Settings().logging.level != "DEBUG"

def test_handle_profile_settings_with_missing_profile_data(
self, monkeypatch, tmp_path
):
profiles_path = tmp_path / "profiles.toml"

monkeypatch.delenv("PREFECT_TESTING_TEST_MODE", raising=False)
monkeypatch.delenv("PREFECT_TESTING_UNIT_TEST_MODE", raising=False)
monkeypatch.setenv("PREFECT_PROFILES_PATH", str(profiles_path))

profiles_path.write_text(
textwrap.dedent(
"""
active = "bar"
"""
)
)

# Should default to ephemeral profile
assert Settings().server.ephemeral.enabled


class TestLoadProfiles:
@pytest.fixture(autouse=True)
Expand Down