Skip to content

refactor: Migrate from is_context_in_segment to get_evaluation_result#6896

Open
khvn26 wants to merge 4 commits intomainfrom
feat/remove-vendored-pydantic/1-is-context-in-segment
Open

refactor: Migrate from is_context_in_segment to get_evaluation_result#6896
khvn26 wants to merge 4 commits intomainfrom
feat/remove-vendored-pydantic/1-is-context-in-segment

Conversation

@khvn26
Copy link
Member

@khvn26 khvn26 commented Mar 9, 2026

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Closes #6669.

In this PR, we migrate the two callers of the vendored is_context_in_segment wrapper to use flag-engine v10's public API get_evaluation_result directly. This is the first step towards removing the vendored Pydantic models in util/engine_models/.

Changes:

  • Identity.get_segments() now builds an EvaluationContext via the new map_environment_to_evaluation_context mapper and evaluates all segments in a single get_evaluation_result call, replacing the per-segment loop through is_context_in_segment.

  • SegmentSerializer.get_member() in the webhook integration is updated similarly.

  • New mapper functions in util/mappers/engine.py: map_environment_to_evaluation_context, map_segment_to_segment_context, map_rule_to_segment_rule, and map_condition_to_segment_condition — constructing flag-engine TypedDicts directly from Django ORM models. TypeAdapters are used only for literal types (RuleType, ConditionOperator).

  • SegmentEngineMetadata TypedDict in segments/types.py carries the segment PK through evaluation metadata, enabling unambiguous matching of results back to Django Segment objects (necessary when segments share the same name).

  • Removed map_engine_identity_to_context (the environment-less evaluation context mapper) — no longer needed.

  • Removed environments/identities/helpers.py and its duplicate get_hashed_percentage_for_object_ids (0–1 range). features/models.py now imports directly from flag_engine.utils.hashing (0–100 range), eliminating the * 100 multiplication. Percentage-split tests simplified to use deterministic threshold values (100 for always-in-segment, 0 for never-in-segment) instead of computing hash values.

How did you test this code?

  • Existing tests pass.
  • Added unit tests for all new mappers with 100% diff coverage.

…1/5)

In this PR, we migrate the two callers of the vendored
`is_context_in_segment` wrapper to use flag-engine v10's public API
`get_evaluation_result` directly. This is the first step towards
removing the vendored Pydantic models in `util/engine_models/`.

Changes:

- `Identity.get_segments()` now builds an `EvaluationContext` via the
  new `map_environment_to_evaluation_context` mapper and evaluates all
  segments in a single `get_evaluation_result` call, replacing the
  per-segment loop through `is_context_in_segment`.

- `SegmentSerializer.get_member()` in the webhook integration is
  updated similarly.

- New mapper functions in `util/mappers/engine.py`:
  `map_environment_to_evaluation_context`,
  `map_segment_to_segment_context`, `map_rule_to_segment_rule`, and
  `map_condition_to_segment_condition` — constructing flag-engine
  TypedDicts directly from Django ORM models. TypeAdapters are used
  only for literal types (`RuleType`, `ConditionOperator`).

- `SegmentEngineMetadata` TypedDict in `segments/types.py` carries
  the segment PK through evaluation metadata, enabling unambiguous
  matching of results back to Django `Segment` objects (necessary
  when segments share the same name).

- Removed `map_engine_identity_to_context` (the environment-less
  evaluation context mapper) — no longer needed.

- Unit tests for all new mappers with 100% diff coverage.
@khvn26 khvn26 requested a review from a team as a code owner March 9, 2026 17:59
@khvn26 khvn26 requested review from emyller and removed request for a team March 9, 2026 17:59
@vercel
Copy link

vercel bot commented Mar 9, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs Ignored Ignored Preview Mar 9, 2026 7:34pm
flagsmith-frontend-preview Ignored Ignored Preview Mar 9, 2026 7:34pm
flagsmith-frontend-staging Ignored Ignored Preview Mar 9, 2026 7:34pm

Request Review

@github-actions github-actions bot added api Issue related to the REST API feature New feature or request labels Mar 9, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2026

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-e2e:pr-6896 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api-test:pr-6896 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-6896 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api:pr-6896 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-6896 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-6896 Finished ✅ Results

@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2026

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  10 passed

Details

stats  10 tests across 7 suites
duration  27.7 seconds
commit  898ddc9
info  🔄 Run: #15187 (attempt 1)

@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2026

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  10 passed

Details

stats  10 tests across 7 suites
duration  54.1 seconds
commit  898ddc9
info  🔄 Run: #15187 (attempt 1)

@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2026

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  53.2 seconds
commit  898ddc9
info  🔄 Run: #15187 (attempt 1)

@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2026

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  16 passed

Details

stats  16 tests across 13 suites
duration  29.1 seconds
commit  898ddc9
info  🔄 Run: #15187 (attempt 1)

@github-actions github-actions bot added feature New feature or request and removed feature New feature or request labels Mar 9, 2026
@khvn26 khvn26 changed the title feat: Migrate from is_context_in_segment to get_evaluation_result refactor: Migrate from is_context_in_segment to get_evaluation_result Mar 9, 2026
@codecov
Copy link

codecov bot commented Mar 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.33%. Comparing base (48de147) to head (898ddc9).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6896      +/-   ##
==========================================
- Coverage   98.34%   98.33%   -0.01%     
==========================================
  Files        1335     1334       -1     
  Lines       49707    49694      -13     
==========================================
- Hits        48882    48867      -15     
- Misses        825      827       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions bot added refactor and removed feature New feature or request labels Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate from is_context_in_segment to get_evaluation_result

1 participant