Skip to content

Fixing Targeting Context Groups - #81

Merged
Matthew Metcalf (mrm9084) merged 3 commits into
mainfrom
FixTargetingContextGroups
Nov 10, 2026
Merged

Matthew Metcalf (mrm9084) merged 3 commits into
mainfrom
FixTargetingContextGroups

Conversation

@mrm9084

Copy link
Copy Markdown
Member

Description

This pull request refactors the TargetingContext model to improve safety and usability, especially around default values and mutability, and adds comprehensive tests to verify its behavior. The main changes are the introduction of a safer default for the groups field, prevention of accidental shared mutable state, and thorough testing of these improvements.

TargetingContext Model Improvements

  • Refactored TargetingContext to inherit from a new _TargetingContextTuple base, and changed the groups field from Sequence[str] to list[str], ensuring each instance gets its own list and avoiding shared mutable defaults. The constructor now handles groups=None by creating a new empty list. (featuremanagement/_models/_targeting_context.pyL8-R44)

Safety and Mutability

Comprehensive Testing

  • Introduced a suite of tests in test_default_feature_flags.py to verify:
    • Field defaults match constructor behavior and are independent between instances.
    • Fields are read-only and immutable after construction.
    • Serialization and deserialization (JSON and pickle) work as expected and preserve instance data.
    • Helper methods like _make and _replace maintain subclass type and data integrity.
    • Explicit checks that default and supplied groups lists are independent and not shared. (tests/test_default_feature_flags.pyR8-R81)

Integration Tests

  • Added integration-style tests to both synchronous and asynchronous test suites to confirm that modifying the groups list after construction affects feature flag evaluation as expected, and that default groups lists remain independent. ([1], [2])

Please add an informative description that covers that changes made by the pull request and link all relevant issues.

Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.
  • Pull request includes test coverage for the included changes.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR.

Copilot AI 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 recommended

The public groups parameter now rejects previously valid typed sequence callers, while unchanged runtime storage makes the list[str] field annotation inaccurate for non-list inputs.

1 open issue
What changed in this PR

This PR updates TargetingContext to provide a fresh, mutable groups list when groups are omitted or None, with regression coverage for model behavior and feature evaluation.

Changes:

  • Introduces a named-tuple base and custom constructor while preserving supplied list identity.
  • Tests field defaults, read-only attributes, serialization, and tuple helpers.
  • Verifies group mutation and default-list isolation in synchronous and asynchronous feature evaluation.
File Description
tests/​test_default_feature_flags.py Adds model regression tests and synchronous targeting checks.
tests/​test_default_feature_flags_async.py Adds asynchronous targeting checks for mutable, independent groups.
featuremanagement/​_models/​_targeting_context.py Adds per-instance default groups lists through a custom constructor.

Review effort: Balanced

@mrm9084
Matthew Metcalf (mrm9084) merged commit d775399 into main Nov 10, 2026
8 checks passed
@mrm9084
Matthew Metcalf (mrm9084) deleted the FixTargetingContextGroups branch November 10, 2026 22:26
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.

4 participants