Skip to content

Commit 0076b66

Browse files
feat: Expose get_feature_flag_result method in public API (#284)
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 09dad81 commit 0076b66

File tree

4 files changed

+141
-67
lines changed

4 files changed

+141
-67
lines changed

example.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,19 @@
5959
# get payload
6060
print(posthog.get_feature_flag_payload("beta-feature", "distinct_id"))
6161
print(posthog.get_all_flags_and_payloads("distinct_id"))
62-
exit()
63-
# # Alias a previous distinct id with a new one
62+
63+
# get feature flag result with all details (enabled, variant, payload, key, reason)
64+
result = posthog.get_feature_flag_result("beta-feature", "distinct_id")
65+
if result:
66+
print(f"Flag key: {result.key}")
67+
print(f"Flag enabled: {result.enabled}")
68+
print(f"Variant: {result.variant}")
69+
print(f"Payload: {result.payload}")
70+
print(f"Reason: {result.reason}")
71+
# get_value() returns the variant if it exists, otherwise the enabled value
72+
print(f"Value (variant or enabled): {result.get_value()}")
73+
74+
# Alias a previous distinct id with a new one
6475

6576
posthog.alias("distinct_id", "new_distinct_id")
6677

posthog/__init__.py

Lines changed: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
set_context_session as inner_set_context_session,
1212
identify_context as inner_identify_context,
1313
)
14-
from posthog.types import FeatureFlag, FlagsAndPayloads
14+
from posthog.types import FeatureFlag, FlagsAndPayloads, FeatureFlagResult
1515
from posthog.version import VERSION
1616

1717
__version__ = VERSION
@@ -388,9 +388,9 @@ def capture_exception(
388388
def feature_enabled(
389389
key, # type: str
390390
distinct_id, # type: str
391-
groups={}, # type: dict
392-
person_properties={}, # type: dict
393-
group_properties={}, # type: dict
391+
groups=None, # type: Optional[dict]
392+
person_properties=None, # type: Optional[dict]
393+
group_properties=None, # type: Optional[dict]
394394
only_evaluate_locally=False, # type: bool
395395
send_feature_flag_events=True, # type: bool
396396
disable_geoip=None, # type: Optional[bool]
@@ -427,9 +427,9 @@ def feature_enabled(
427427
"feature_enabled",
428428
key=key,
429429
distinct_id=distinct_id,
430-
groups=groups,
431-
person_properties=person_properties,
432-
group_properties=group_properties,
430+
groups=groups or {},
431+
person_properties=person_properties or {},
432+
group_properties=group_properties or {},
433433
only_evaluate_locally=only_evaluate_locally,
434434
send_feature_flag_events=send_feature_flag_events,
435435
disable_geoip=disable_geoip,
@@ -439,9 +439,9 @@ def feature_enabled(
439439
def get_feature_flag(
440440
key, # type: str
441441
distinct_id, # type: str
442-
groups={}, # type: dict
443-
person_properties={}, # type: dict
444-
group_properties={}, # type: dict
442+
groups=None, # type: Optional[dict]
443+
person_properties=None, # type: Optional[dict]
444+
group_properties=None, # type: Optional[dict]
445445
only_evaluate_locally=False, # type: bool
446446
send_feature_flag_events=True, # type: bool
447447
disable_geoip=None, # type: Optional[bool]
@@ -477,9 +477,9 @@ def get_feature_flag(
477477
"get_feature_flag",
478478
key=key,
479479
distinct_id=distinct_id,
480-
groups=groups,
481-
person_properties=person_properties,
482-
group_properties=group_properties,
480+
groups=groups or {},
481+
person_properties=person_properties or {},
482+
group_properties=group_properties or {},
483483
only_evaluate_locally=only_evaluate_locally,
484484
send_feature_flag_events=send_feature_flag_events,
485485
disable_geoip=disable_geoip,
@@ -488,9 +488,9 @@ def get_feature_flag(
488488

489489
def get_all_flags(
490490
distinct_id, # type: str
491-
groups={}, # type: dict
492-
person_properties={}, # type: dict
493-
group_properties={}, # type: dict
491+
groups=None, # type: Optional[dict]
492+
person_properties=None, # type: Optional[dict]
493+
group_properties=None, # type: Optional[dict]
494494
only_evaluate_locally=False, # type: bool
495495
disable_geoip=None, # type: Optional[bool]
496496
) -> Optional[dict[str, FeatureFlag]]:
@@ -520,21 +520,64 @@ def get_all_flags(
520520
return _proxy(
521521
"get_all_flags",
522522
distinct_id=distinct_id,
523-
groups=groups,
524-
person_properties=person_properties,
525-
group_properties=group_properties,
523+
groups=groups or {},
524+
person_properties=person_properties or {},
525+
group_properties=group_properties or {},
526526
only_evaluate_locally=only_evaluate_locally,
527527
disable_geoip=disable_geoip,
528528
)
529529

530530

531+
def get_feature_flag_result(
532+
key,
533+
distinct_id,
534+
groups=None, # type: Optional[dict]
535+
person_properties=None, # type: Optional[dict]
536+
group_properties=None, # type: Optional[dict]
537+
only_evaluate_locally=False,
538+
send_feature_flag_events=True,
539+
disable_geoip=None, # type: Optional[bool]
540+
):
541+
# type: (...) -> Optional[FeatureFlagResult]
542+
"""
543+
Get a FeatureFlagResult object which contains the flag result and payload.
544+
545+
This method evaluates a feature flag and returns a FeatureFlagResult object containing:
546+
- enabled: Whether the flag is enabled
547+
- variant: The variant value if the flag has variants
548+
- payload: The payload associated with the flag (automatically deserialized from JSON)
549+
- key: The flag key
550+
- reason: Why the flag was enabled/disabled
551+
552+
Example:
553+
```python
554+
result = posthog.get_feature_flag_result('beta-feature', 'distinct_id')
555+
if result and result.enabled:
556+
# Use the variant and payload
557+
print(f"Variant: {result.variant}")
558+
print(f"Payload: {result.payload}")
559+
```
560+
"""
561+
return _proxy(
562+
"get_feature_flag_result",
563+
key=key,
564+
distinct_id=distinct_id,
565+
groups=groups or {},
566+
person_properties=person_properties or {},
567+
group_properties=group_properties or {},
568+
only_evaluate_locally=only_evaluate_locally,
569+
send_feature_flag_events=send_feature_flag_events,
570+
disable_geoip=disable_geoip,
571+
)
572+
573+
531574
def get_feature_flag_payload(
532575
key,
533576
distinct_id,
534577
match_value=None,
535-
groups={},
536-
person_properties={},
537-
group_properties={},
578+
groups=None, # type: Optional[dict]
579+
person_properties=None, # type: Optional[dict]
580+
group_properties=None, # type: Optional[dict]
538581
only_evaluate_locally=False,
539582
send_feature_flag_events=True,
540583
disable_geoip=None, # type: Optional[bool]
@@ -544,9 +587,9 @@ def get_feature_flag_payload(
544587
key=key,
545588
distinct_id=distinct_id,
546589
match_value=match_value,
547-
groups=groups,
548-
person_properties=person_properties,
549-
group_properties=group_properties,
590+
groups=groups or {},
591+
person_properties=person_properties or {},
592+
group_properties=group_properties or {},
550593
only_evaluate_locally=only_evaluate_locally,
551594
send_feature_flag_events=send_feature_flag_events,
552595
disable_geoip=disable_geoip,
@@ -575,18 +618,18 @@ def get_remote_config_payload(
575618

576619
def get_all_flags_and_payloads(
577620
distinct_id,
578-
groups={},
579-
person_properties={},
580-
group_properties={},
621+
groups=None, # type: Optional[dict]
622+
person_properties=None, # type: Optional[dict]
623+
group_properties=None, # type: Optional[dict]
581624
only_evaluate_locally=False,
582625
disable_geoip=None, # type: Optional[bool]
583626
) -> FlagsAndPayloads:
584627
return _proxy(
585628
"get_all_flags_and_payloads",
586629
distinct_id=distinct_id,
587-
groups=groups,
588-
person_properties=person_properties,
589-
group_properties=group_properties,
630+
groups=groups or {},
631+
person_properties=person_properties or {},
632+
group_properties=group_properties or {},
590633
only_evaluate_locally=only_evaluate_locally,
591634
disable_geoip=disable_geoip,
592635
)

0 commit comments

Comments
 (0)