Skip to content
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
8 changes: 8 additions & 0 deletions openfeature/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ def __init__(
def provider(self) -> FeatureProvider:
return provider_registry.get_provider(self.domain)

@property
def evaluation_context(self) -> EvaluationContext:
return self.context

@evaluation_context.setter
def evaluation_context(self, evaluation_context: EvaluationContext | None) -> None:
self.context = evaluation_context or EvaluationContext()

def get_provider_status(self) -> ProviderStatus:
return provider_registry.get_provider_status(self.provider)

Expand Down
26 changes: 26 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,29 @@ def test_should_noop_if_provider_does_not_support_tracking(monkeypatch):
set_provider(provider)
client = get_client()
client.track(tracking_event_name="test")


def test_client_should_allow_getting_and_setting_evaluation_context():
# Given
client = get_client()
initial_context = client.evaluation_context
assert isinstance(initial_context, EvaluationContext)

new_context = EvaluationContext(
targeting_key="user_123", attributes={"beta_user": True}
)

# When
client.evaluation_context = new_context

# Then
assert client.evaluation_context == new_context
assert client.context == new_context

# When setting to None (resetting)
client.evaluation_context = None

# Then it should default to an empty EvaluationContext
assert isinstance(client.evaluation_context, EvaluationContext)
assert client.evaluation_context.targeting_key is None
assert not client.evaluation_context.attributes