Skip to content

feat: Support $.flags context values in segment conditions - #261

Open
khvn26 wants to merge 4 commits into
mainfrom
feat/flags-context-values
Open

khvn26 wants to merge 4 commits into
mainfrom
feat/flags-context-values

Conversation

@khvn26

@khvn26 khvn26 commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Closes #245.

In this PR, we allow local evaluation's get_environment_flags to process segments without identity-specific segment conditions. In other words, if a segment only contains $.flags.* and $.environment.* conditions, it will apply to environment flags.

Lazy evaluation, introduced in #200, is now skipped for environments containing segments with $.flags.* conditions.

@khvn26
khvn26 requested a review from a team as a code owner October 16, 2026 17:18
@khvn26
khvn26 requested review from emyller and removed request for a team October 16, 2026 17:18
@khvn26
khvn26 marked this pull request as draft October 16, 2026 17:19
@khvn26
khvn26 force-pushed the feat/flags-context-values branch from 35891e0 to a029f90 Compare October 19, 2026 10:22
@khvn26
khvn26 changed the base branch from main to perf/local-eval-regression October 19, 2026 10:22
@khvn26
khvn26 force-pushed the feat/flags-context-values branch 2 times, most recently from aa94f73 to 8775019 Compare October 19, 2026 10:31
@khvn26
khvn26 marked this pull request as ready for review October 19, 2026 11:59
@khvn26
khvn26 force-pushed the feat/flags-context-values branch from b2d63e6 to f1af8bb Compare October 20, 2026 16:10
Base automatically changed from perf/local-eval-regression to main October 21, 2026 11:56
Environment flags are now evaluated with segments whose conditions all read
`$.environment` or `$.flags`, as `GET /api/v1/flags/` does since
Flagsmith/flagsmith#8576. Segments reading traits or identity context are
left out.

When segment conditions read `$.flags`, identity flags are evaluated at
once against the full context, as a context trimmed to one feature lacks
the flags it depends on. Otherwise, they are still evaluated lazily.

Closes #245
`_get_context_reads` and `LocalEvaluation.__init__` both combined reads
through booleans, then built a `_ContextReads` from them. `_ContextReads`
now combines with `|`, from empty defaults.
Renames `_get_context_reads` to `_get_context_reads_from_rules`, and adds
`_get_context_reads_from_segments` for `LocalEvaluation.reads`, as
suggested in review.
…lags

Identity-free segments are only used to evaluate environment flags, at
most once per environment document. They are now filtered there, rather
than eagerly for every environment document.
@khvn26
khvn26 force-pushed the feat/flags-context-values branch from 60b5a5f to cedc5de Compare October 21, 2026 12:10

@emyller emyller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One change requested. Tests look a bit sloppy.

Comment on lines +118 to +127
"segments": {
segment_key: segment_context
for segment_key, segment_context in (
self.context.get("segments") or {}
).items()
if not _get_context_reads_from_rules(
segment_context["rules"]
).identity
},
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we please move this into a private function named after why it the filtering is needed? This is easy to understand while reviewing this PR, but not so much for someone reading this for the first time in a couple years from now.

Comment thread tests/test_flagsmith.py
Comment on lines +381 to +383
assert [segment["name"] for segment in (context["segments"] or {}).values()] == [
"Test segment"
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert [segment["name"] for segment in (context["segments"] or {}).values()] == [
"Test segment"
]
assert [segment["name"] for segment in context["segments"].values()] == [
"Test segment"
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support $.flags context values in segment conditions

2 participants