MCPcopy Create free account
hub / github.com/Flagsmith/flagsmith / variant_assignment

Function variant_assignment

api/tests/unit/experimentation/test_services.py:1827–1848  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1825 identity_hash_keys = [f"identity-{i}" for i in range(50)]
1826
1827 def variant_assignment() -> dict[str, int]:
1828 override = (
1829 FeatureState.objects.get_live_feature_states(
1830 environment=experiment.environment,
1831 additional_filters=Q(
1832 feature_segment__segment=experiment.rollout_segment,
1833 identity__isnull=True,
1834 ),
1835 feature_id=experiment.feature_id,
1836 )
1837 .prefetch_related(
1838 "multivariate_feature_state_values__multivariate_feature_option"
1839 )
1840 .latest("id")
1841 )
1842 assignment: dict[str, int] = {}
1843 for key in identity_hash_keys:
1844 option = override.get_multivariate_feature_state_value(key)
1845 # The 50/50 split allocates 100%, so every identity lands on an option.
1846 assert isinstance(option, MultivariateFeatureOption)
1847 assignment[key] = option.id
1848 return assignment
1849
1850 # When the rollout is applied, then re-applied unchanged (e.g. tuned while
1851 # the experiment is running)

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…