Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

feat: allow setting bandits initial configuration #72

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions eppo_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def init(config: Config) -> EppoClient:
flag_config_store.set_configurations(
config.initial_configuration._flags_configuration.flags
)
if config.initial_configuration._bandits_configuration:
bandit_config_store.set_configurations(
config.initial_configuration._bandits_configuration.bandits
)

config_requestor = ExperimentConfigurationRequestor(
http_client=http_client,
Expand Down
15 changes: 13 additions & 2 deletions eppo_client/configuration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from eppo_client.models import UfcResponse
from typing import Union
from eppo_client.models import UfcResponse, BanditResponse


class Configuration:
Expand All @@ -7,5 +8,15 @@ class Configuration:
interpret feature flags.
"""

def __init__(self, flags_configuration: str):
def __init__(
self,
flags_configuration: Union[bytes, str],
bandits_configuration: Union[bytes, str, None] = None,
) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

self._flags_configuration = UfcResponse.model_validate_json(flags_configuration)

self._bandits_configuration = None
if bandits_configuration is not None:
self._bandits_configuration = BanditResponse.model_validate_json(
bandits_configuration
)
4 changes: 4 additions & 0 deletions eppo_client/configuration_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ def _set_configuration(self, configuration: Configuration):
self.__flag_config_store.set_configurations(
configuration._flags_configuration.flags
)
if configuration._bandits_configuration is not None:
self.__bandit_config_store.set_configurations(
configuration._bandits_configuration.bandits
)
2 changes: 1 addition & 1 deletion eppo_client/configuration_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class ConfigurationStore(Generic[T]):
def __init__(self):
def __init__(self) -> None:
self.__is_initialized = False
self.__cache: Dict[str, T] = {}
self.__lock = ReadWriteLock()
Expand Down
4 changes: 4 additions & 0 deletions eppo_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ class BanditData(SdkBaseModel):
bandit_model_version: str = Field(alias="modelVersion")
bandit_model_data: BanditModelData = Field(alias="modelData")
updated_at: datetime


class BanditResponse(SdkBaseModel):
bandits: Dict[str, BanditData]
2 changes: 1 addition & 1 deletion eppo_client/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Note to developers: When ready to bump to 4.0, please change
# the `POLL_INTERVAL_SECONDS` constant in `eppo_client/constants.py`
# to 30 seconds to match the behavior of the other server SDKs.
__version__ = "3.7.0"
__version__ = "3.8.0"
4 changes: 2 additions & 2 deletions test/configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def test_init_valid():
Configuration(flags_configuration='{"flags": {}}')
Configuration(flags_configuration=b'{"flags": {}}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're allowing bytes or a string, I imagine we should test both.

Also, let's have another test for initialization with a provided bandit configuration so we know it works!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String is essentially deprecated and removed in 4.0.0



def test_init_invalid_json():
Expand All @@ -15,4 +15,4 @@ def test_init_invalid_json():

def test_init_invalid_format():
with pytest.raises(pydantic.ValidationError):
Configuration(flags_configuration='{"flags": []}')
Configuration(flags_configuration=b'{"flags": []}')
Loading