Skip to content

feat: Context values for Segments - #220

Merged
khvn26 merged 11 commits into
mainfrom
feat/context-values
Jul 21, 2025
Merged

feat: Context values for Segments#220
khvn26 merged 11 commits into
mainfrom
feat/context-values

Conversation

@khvn26

Copy link
Copy Markdown
Member

Closes#219.

@khvn26
khvn26 requested review from gagantrivedi and matthewelwell and removed request for gagantrivediJuly 16, 2025 19:15
@github-actions

github-actionsBot commented Jul 16, 2025

Copy link
Copy Markdown

FileCoverage
All files100%

Minimum allowed coverage is 100%

Generated by 🐒 cobertura-action against 58e833a

@emylleremyller 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.

Changes look great. Minor questions.

Comment thread.gitmodules
[submodule "tests/engine_tests/engine-test-data"]
path = tests/engine_tests/engine-test-data
url = https://github.com/flagsmith/engine-test-data.git
branch = feat/context-values

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.

Reminder to delete.

@khvn26khvn26Jul 17, 2025

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

A discussion point as well as merging Flagsmith/engine-test-data#8 will break CI for all local eval SDKs. It's a good incentive for closing Flagsmith/flagsmith#5676 ASAP, of course, but perhaps we should consider versioning engine-test-data.

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.

Do you mean hard versions? e.g. v1/ and v2/, etc? I support temporary branch pointing at main — given a reasonable plan — unless there's value in the former.

@khvn26khvn26Jul 18, 2025

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

I meant using version tags in engine-test-data and referencing them instead of main branch in submodules.

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.

Interesting. Yes, I think it makes sense to version the test data.

I guess the original thinking behind not versioning it, is that the test data was to be used for more of a regression test, but I don't think that makes sense here, so I'm happy to go with a versioned approach.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Created https://github.com/Flagsmith/engine-test-data/releases/tag/v1.0.0 for the current SDKs to point to 👍

Comment threadflag_engine/context/types.py
Comment threadflag_engine/engine.py Outdated
Comment threadflag_engine/engine.py Outdated
Comment threadflag_engine/segments/evaluator.py Outdated
Comment threadflag_engine/segments/evaluator.py
Comment threadflag_engine/segments/evaluator.py Outdated
context_value: TraitValue,
) -> bool:
return isinstance(trait_value, str) and str(segment_value) not in trait_value
return isinstance(context_value, str) and str(segment_value) not in context_value

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.

Unrelated with this PR but I wonder if we should really "None" not in context_value if segment_value is null.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Nice catch, let's come up with a test case for this (outside of this PR)?

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.

I'm writing an issue but is there any use case where segment_value is None other than PERCENTAGE_SPLIT? Do you see how to reproduce this with a real scenario? I think potentially this has never been a problem.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Yeah, I don't think we can have an IN segment with a null value, realistically.

@khvn26
khvn26force-pushed the feat/context-values branch from ea3f342 to 93c2310CompareJuly 17, 2025 23:27
@khvn26
khvn26force-pushed the feat/context-values branch from 93c2310 to 42829faCompareJuly 17, 2025 23:45
@khvn26
khvn26 requested a review from emyllerJuly 17, 2025 23:49
Comment threadflag_engine/segments/evaluator.py Outdated

@matthewelwellmatthewelwell 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.

Looks neat overall, added a couple of comments, and keen to see the resolutions on the outstanding comments from @emyller .

Comment threadflag_engine/context/mappers.py
Comment thread.gitmodules
[submodule "tests/engine_tests/engine-test-data"]
path = tests/engine_tests/engine-test-data
url = https://github.com/flagsmith/engine-test-data.git
branch = feat/context-values

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.

Interesting. Yes, I think it makes sense to version the test data.

I guess the original thinking behind not versioning it, is that the test data was to be used for more of a regression test, but I don't think that makes sense here, so I'm happy to go with a versioned approach.

Comment threadflag_engine/engine.py Outdated
)
for rule in segment.rules
return bool(rules := segment.rules) and all(
context_matches_rule(context=context, rule=rule, segment_key=segment.id)

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.

This part is particularly interesting (cc @gagantrivedi). This is something that I was chatting to Gagan about regarding the release pipelines work - the reason being is that we can't currently implement a phased rollout using the functionality available in release pipelines since we'd need to use separate segments for each percentage increment. If we allow users to link segments using some kind of other key, then we'd be able to achieve this.

All this is to say, I like the abstraction of segment_key - I don't think there's any changes necessary here right now, I'm just calling it out for Gagan's attention more than anything.

I also don't know if using multiple segments is the right approach for phased rollouts anyway!

Comment threadtests/unit/segments/test_segments_evaluator.py Outdated

@emylleremyller 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.

Agora vai

@khvn26
khvn26 merged commit 01e7a56 into mainJul 21, 2025
6 checks passed
@khvn26
khvn26 deleted the feat/context-values branch July 21, 2025 13:21
@khvn26khvn26 mentioned this pull request Jul 21, 2025
Sign up for freeto 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.

Implement Context values for Segments

3 participants

@khvn26@emyller@matthewelwell