Skip to content

Commit

Permalink
Fix settings of cache provider by environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeflandre committed Feb 2, 2024
1 parent e06b83a commit 9b979bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
18 changes: 7 additions & 11 deletions modelkit/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,15 @@ class NativeCacheSettings(CacheSettings):

def cache_settings():
s = CacheSettings()
if s.cache_provider is None:

if s.cache_provider == "none":
return None
try:
elif s.cache_provider == "redis":
return RedisSettings()
except pydantic.ValidationError:
pass
try:
elif s.cache_provider == "native":
return NativeCacheSettings()
except pydantic.ValidationError:
pass
else:
return None


def _get_library_settings_cache_provider(v: Optional[str]) -> str:
Expand Down Expand Up @@ -134,8 +133,5 @@ class LibrarySettings(ModelkitSettings):
Annotated[None, pydantic.Tag("none")],
],
pydantic.Discriminator(_get_library_settings_cache_provider),
] = pydantic.Field(
default_factory=cache_settings,
union_mode="left_to_right",
)
] = pydantic.Field(default_factory=cache_settings)
model_config = pydantic.ConfigDict(extra="allow")
13 changes: 5 additions & 8 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@ def test_cache_provider_settings(monkeypatch):
assert lib_settings.cache.cache_provider == "redis"

monkeypatch.setenv("MODELKIT_CACHE_PROVIDER", "native")
with pytest.raises(pydantic.ValidationError): # FIXME: shouldn't raise
lib_settings = LibrarySettings()
assert isinstance(lib_settings.cache, NativeCacheSettings)
assert lib_settings.cache.cache_provider == "native"
lib_settings = LibrarySettings()
assert isinstance(lib_settings.cache, NativeCacheSettings)
assert lib_settings.cache.cache_provider == "native"

monkeypatch.setenv("MODELKIT_CACHE_PROVIDER", "none")
with pytest.raises(pydantic.ValidationError): # FIXME: shouldn't raise
assert LibrarySettings().cache is None
assert LibrarySettings().cache is None

monkeypatch.setenv("MODELKIT_CACHE_PROVIDER", "not supported")
with pytest.raises(pydantic.ValidationError): # FIXME: shouldn't raise
assert LibrarySettings().cache is None
assert LibrarySettings().cache is None

monkeypatch.delenv("MODELKIT_CACHE_PROVIDER")
assert LibrarySettings().cache is None

0 comments on commit 9b979bc

Please sign in to comment.