Skip to content

Commit

Permalink
Fix type hint for lazy property (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter authored Aug 19, 2024
1 parent a6e3af8 commit aeb97f5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 53 deletions.
10 changes: 3 additions & 7 deletions griptape/config/base_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from abc import ABC
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING

from attrs import define, field

Expand All @@ -14,9 +14,5 @@

@define(kw_only=True)
class BaseConfig(SerializableMixin, ABC):
_logging_config: Optional[LoggingConfig] = field(alias="logging")
_driver_config: Optional[BaseDriverConfig] = field(alias="drivers")

def reset(self) -> None:
self._logging_config = None
self._driver_config = None
logging_config: LoggingConfig = field()
driver_config: BaseDriverConfig = field()
18 changes: 4 additions & 14 deletions griptape/config/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING

from attrs import define, field

from griptape.utils.decorators import lazy_property
from attrs import Factory, define, field

from .base_config import BaseConfig
from .drivers.openai_driver_config import OpenAiDriverConfig
Expand All @@ -16,16 +14,8 @@

@define(kw_only=True)
class _Config(BaseConfig):
_logging_config: Optional[LoggingConfig] = field(default=None, alias="logging")
_driver_config: Optional[BaseDriverConfig] = field(default=None, alias="drivers")

@lazy_property()
def driver_config(self) -> BaseDriverConfig:
return OpenAiDriverConfig()

@lazy_property()
def logging_config(self) -> LoggingConfig:
return LoggingConfig()
logging_config: LoggingConfig = field(default=Factory(lambda: LoggingConfig()))
driver_config: BaseDriverConfig = field(default=Factory(lambda: OpenAiDriverConfig()))


config = _Config()
4 changes: 2 additions & 2 deletions griptape/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def wrapper(self: Any, *args, **kwargs) -> Any:
return decorator


def lazy_property(attr_name: Optional[str] = None) -> Any:
def decorator(func: Callable) -> Any:
def lazy_property(attr_name: Optional[str] = None) -> Callable[[Callable[[Any], Any]], property]:
def decorator(func: Callable[[Any], Any]) -> property:
actual_attr_name = f"_{func.__name__}" if attr_name is None else attr_name

@property
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/config/drivers/test_driver_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,34 @@ def test_dot_update(self, config):
config.prompt_driver.max_tokens = 10

assert config.prompt_driver.max_tokens == 10

@pytest.mark.skip_mock_config()
def test_lazy_init(self):
from griptape.config import config

assert config.driver_config._prompt_driver is None
assert config.driver_config._image_generation_driver is None
assert config.driver_config._image_query_driver is None
assert config.driver_config._embedding_driver is None
assert config.driver_config._vector_store_driver is None
assert config.driver_config._conversation_memory_driver is None
assert config.driver_config._text_to_speech_driver is None
assert config.driver_config._audio_transcription_driver is None

assert config.driver_config.prompt_driver is not None
assert config.driver_config.image_generation_driver is not None
assert config.driver_config.image_query_driver is not None
assert config.driver_config.embedding_driver is not None
assert config.driver_config.vector_store_driver is not None
assert config.driver_config.conversation_memory_driver is None
assert config.driver_config.text_to_speech_driver is not None
assert config.driver_config.audio_transcription_driver is not None

assert config.driver_config._prompt_driver is not None
assert config.driver_config._image_generation_driver is not None
assert config.driver_config._image_query_driver is not None
assert config.driver_config._embedding_driver is not None
assert config.driver_config._vector_store_driver is not None
assert config.driver_config._conversation_memory_driver is None
assert config.driver_config._text_to_speech_driver is not None
assert config.driver_config._audio_transcription_driver is not None
26 changes: 0 additions & 26 deletions tests/unit/config/test_config.py

This file was deleted.

4 changes: 0 additions & 4 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def mock_event_bus():
def mock_config(request):
from griptape.config import config

config.reset()

# Some tests we don't want to use the autouse fixture's MockDriverConfig
if "skip_mock_config" in request.keywords:
yield
Expand All @@ -29,5 +27,3 @@ def mock_config(request):
config.driver_config = MockDriverConfig()

yield config

config.reset()

0 comments on commit aeb97f5

Please sign in to comment.