@@ -36,6 +36,7 @@ def test_flag_from_evaluation_result() -> None:
3636 assert flag .feature_id == 123
3737 assert flag .is_default is False
3838 assert flag .variant == "control"
39+ assert flag .reason == "DEFAULT"
3940
4041
4142def test_flag_from_evaluation_result__no_variant__is_none () -> None :
@@ -218,6 +219,37 @@ def test_flag_from_api_flag__no_variant__is_none() -> None:
218219 assert flag .variant is None
219220
220221
222+ def test_flag_from_api_flag__sets_reason () -> None :
223+ # Given
224+ flag_data = {
225+ "enabled" : True ,
226+ "feature_state_value" : "test-value" ,
227+ "feature" : {"name" : "test_feature" , "id" : 123 },
228+ "reason" : "TARGETING_MATCH; segment=premium" ,
229+ }
230+
231+ # When
232+ flag = Flag .from_api_flag (flag_data )
233+
234+ # Then
235+ assert flag .reason == "TARGETING_MATCH; segment=premium"
236+
237+
238+ def test_flag_from_api_flag__no_reason__is_none () -> None :
239+ # Given
240+ flag_data = {
241+ "enabled" : True ,
242+ "feature_state_value" : "test-value" ,
243+ "feature" : {"name" : "test_feature" , "id" : 123 },
244+ }
245+
246+ # When
247+ flag = Flag .from_api_flag (flag_data )
248+
249+ # Then
250+ assert flag .reason is None
251+
252+
221253def test_get_flag_without_pipeline_processor () -> None :
222254 flags = Flags (
223255 flags = {
@@ -335,8 +367,10 @@ def test_lazy_flags__get_flag__applies_matching_segment_override(
335367 # When: we read the targeted feature.
336368 target = lazy_flags .get_flag ("target" )
337369 # Then: the override wins over the base feature value.
370+ assert isinstance (target , Flag )
338371 assert target .enabled is True
339372 assert target .value == "premium-value"
373+ assert target .reason == "TARGETING_MATCH; segment=premium_segment"
340374
341375
342376def test_lazy_flags__get_flag__skips_non_matching_segment_override (
@@ -355,8 +389,10 @@ def test_lazy_flags__get_flag__skips_non_matching_segment_override(
355389 target = flags .get_flag ("target" )
356390
357391 # Then: the override doesn't win and base-value comes through.
392+ assert isinstance (target , Flag )
358393 assert target .enabled is False
359394 assert target .value == "base-value"
395+ assert target .reason == "DEFAULT"
360396
361397
362398def test_lazy_flags__get_flag__caches_per_feature (
0 commit comments