Skip to content

Commit 840284d

Browse files
emyllerclaude
andcommitted
feat(Flag Dependency): Serve flag-dependent segment overrides in environment flags
Environment flags keep segments that only read `$.environment` or `$.flags`, as the Flagsmith API does, and still leave out segments needing an identity. Runs sdk-spec v0.7.0, which covers environment-level flag dependency. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent bca6cfd commit 840284d

5 files changed

Lines changed: 49 additions & 13 deletions

File tree

‎.github/workflows/sdk-spec.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ permissions:
1111
jobs:
1212
sdk-spec:
1313
# Keep the ref in `uses:` and `spec-ref` in sync.
14-
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.6.0
14+
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.7.0
1515
with:
16-
spec-ref: v0.6.0
16+
spec-ref: v0.7.0
1717
language: python

‎flagsmith/local_evaluation.py‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from functools import cached_property
66

77
from flag_engine import engine
8-
from flag_engine.context.types import SegmentContext
8+
from flag_engine.context.types import SegmentContext, SegmentRule
99

1010
from flagsmith.api.types import EnvironmentModel
1111
from flagsmith.mappers import (
@@ -26,6 +26,13 @@
2626
TraitMapping,
2727
)
2828

29+
IDENTITY_FREE_PROPERTY_PREFIXES = (
30+
"$.environment.",
31+
"$.environment[",
32+
"$.flags.",
33+
"$.flags[",
34+
)
35+
2936

3037
class LocalEvaluation:
3138
"""Per-environment-document state for local evaluation."""
@@ -67,11 +74,20 @@ def from_environment_document(
6774

6875
@cached_property
6976
def environment_flags(self) -> typing.Dict[str, Flag]:
70-
"""Environment flags, which depend on the environment document only."""
71-
context_without_segments = self.context.copy()
72-
context_without_segments.pop("segments", None)
77+
"""Environment flags, which depend on the environment document only.
78+
79+
Evaluated without an identity, so segments reading traits or identity
80+
context are left out.
81+
"""
82+
environment_context = self.context.copy()
83+
if segments := environment_context.get("segments"):
84+
environment_context["segments"] = {
85+
segment_key: segment_context
86+
for segment_key, segment_context in segments.items()
87+
if _is_identity_free(segment_context["rules"])
88+
}
7389
evaluation_result = engine.get_evaluation_result(
74-
context=context_without_segments,
90+
context=environment_context,
7591
)
7692
return {
7793
feature_name: Flag.from_evaluation_result(flag_result)
@@ -113,3 +129,14 @@ def _index_overrides(
113129
for override in segment_context.get("overrides") or ()
114130
if (feature_name := override["name"])
115131
}
132+
133+
134+
def _is_identity_free(rules: typing.Iterable[SegmentRule]) -> bool:
135+
return all(
136+
all(
137+
condition["property"].startswith(IDENTITY_FREE_PROPERTY_PREFIXES)
138+
for condition in rule.get("conditions") or ()
139+
)
140+
and _is_identity_free(rule.get("rules") or ())
141+
for rule in rules
142+
)

‎sdk-spec/steps.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def create_client_with_events(
3939
)
4040

4141

42+
# Create the SDK client with local evaluation
43+
def create_client_with_local_evaluation(
44+
environment_key: str, api_url: str
45+
) -> Flagsmith:
46+
return Flagsmith(
47+
environment_key=environment_key,
48+
api_url=api_url,
49+
enable_local_evaluation=True,
50+
)
51+
52+
4253
# When the application gets the environment flags
4354
def get_environment_flags(flagsmith: Flagsmith) -> list[SpecFlag]:
4455
return as_spec_flags(flagsmith.get_environment_flags())

‎tests/conftest.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ def flag_dependency_context() -> SDKEvaluationContext:
162162
"""
163163
return {
164164
"environment": {"key": "env-key", "name": "env"},
165-
"identity": {"identifier": "user-1", "key": "env-key_user-1"},
166165
"features": {
167166
"transitive": {
168167
"key": "4",

‎tests/test_flagsmith.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_get_environment_flags_uses_local_environment_when_available(
101101
assert all_flags[0].value == "some-value"
102102

103103

104-
def test_get_environment_flags_omits_segments_from_evaluation_context(
104+
def test_get_environment_flags__local_evaluation__omits_segments_needing_an_identity(
105105
mocker: MockerFixture,
106106
local_eval_flagsmith: Flagsmith,
107107
evaluation_context: SDKEvaluationContext,
@@ -131,10 +131,9 @@ def test_get_environment_flags_omits_segments_from_evaluation_context(
131131
local_eval_flagsmith.get_environment_flags()
132132

133133
# Then
134-
# Verify segments are not present in the context passed to the engine
135-
context_without_segments = evaluation_context.copy()
136-
context_without_segments.pop("segments", None)
137-
mock_get_evaluation_result.assert_called_once_with(context=context_without_segments)
134+
mock_get_evaluation_result.assert_called_once_with(
135+
context={**evaluation_context, "segments": {}},
136+
)
138137

139138

140139
def test_get_environment_flags__local_evaluation__evaluates_once_per_environment(

0 commit comments

Comments
 (0)