Skip to content

Commit 95b7df9

Browse files
committed
fix: Replace mutable default parameters with None in feature flag methods
- Changed default parameters from {} to None for all feature flag methods - Prevents potential bugs from shared mutable defaults across function calls - Fully backwards compatible - callers see no change in behavior - Follows Python best practices for function parameter defaults Also: - Removed exit() call from example.py that was preventing remaining examples from running - Expanded get_feature_flag_result example to demonstrate all available fields Addresses review feedback from PR #284
1 parent 92fc801 commit 95b7df9

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

example.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@
6060
print(posthog.get_feature_flag_payload("beta-feature", "distinct_id"))
6161
print(posthog.get_all_flags_and_payloads("distinct_id"))
6262

63-
# get feature flag result with all details (enabled, variant, payload)
63+
# get feature flag result with all details (enabled, variant, payload, key, reason)
6464
result = posthog.get_feature_flag_result("beta-feature", "distinct_id")
6565
if result:
66+
print(f"Flag key: {result.key}")
6667
print(f"Flag enabled: {result.enabled}")
6768
print(f"Variant: {result.variant}")
6869
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()}")
6973

70-
exit()
71-
# # Alias a previous distinct id with a new one
74+
# Alias a previous distinct id with a new one
7275

7376
posthog.alias("distinct_id", "new_distinct_id")
7477

posthog/__init__.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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,9 +520,9 @@ 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
)
@@ -531,23 +531,23 @@ def get_all_flags(
531531
def get_feature_flag_result(
532532
key,
533533
distinct_id,
534-
groups={},
535-
person_properties={},
536-
group_properties={},
534+
groups=None, # type: Optional[dict]
535+
person_properties=None, # type: Optional[dict]
536+
group_properties=None, # type: Optional[dict]
537537
only_evaluate_locally=False,
538538
send_feature_flag_events=True,
539539
disable_geoip=None, # type: Optional[bool]
540540
) -> Optional[FeatureFlagResult]: # type hint for feature flag result
541541
"""
542542
Get a FeatureFlagResult object which contains the flag result and payload.
543-
543+
544544
This method evaluates a feature flag and returns a FeatureFlagResult object containing:
545545
- enabled: Whether the flag is enabled
546546
- variant: The variant value if the flag has variants
547547
- payload: The payload associated with the flag (automatically deserialized from JSON)
548548
- key: The flag key
549549
- reason: Why the flag was enabled/disabled
550-
550+
551551
Example:
552552
```python
553553
result = posthog.get_feature_flag_result('beta-feature', 'distinct_id')
@@ -561,9 +561,9 @@ def get_feature_flag_result(
561561
"get_feature_flag_result",
562562
key=key,
563563
distinct_id=distinct_id,
564-
groups=groups,
565-
person_properties=person_properties,
566-
group_properties=group_properties,
564+
groups=groups or {},
565+
person_properties=person_properties or {},
566+
group_properties=group_properties or {},
567567
only_evaluate_locally=only_evaluate_locally,
568568
send_feature_flag_events=send_feature_flag_events,
569569
disable_geoip=disable_geoip,
@@ -574,9 +574,9 @@ def get_feature_flag_payload(
574574
key,
575575
distinct_id,
576576
match_value=None,
577-
groups={},
578-
person_properties={},
579-
group_properties={},
577+
groups=None, # type: Optional[dict]
578+
person_properties=None, # type: Optional[dict]
579+
group_properties=None, # type: Optional[dict]
580580
only_evaluate_locally=False,
581581
send_feature_flag_events=True,
582582
disable_geoip=None, # type: Optional[bool]
@@ -586,9 +586,9 @@ def get_feature_flag_payload(
586586
key=key,
587587
distinct_id=distinct_id,
588588
match_value=match_value,
589-
groups=groups,
590-
person_properties=person_properties,
591-
group_properties=group_properties,
589+
groups=groups or {},
590+
person_properties=person_properties or {},
591+
group_properties=group_properties or {},
592592
only_evaluate_locally=only_evaluate_locally,
593593
send_feature_flag_events=send_feature_flag_events,
594594
disable_geoip=disable_geoip,
@@ -617,18 +617,18 @@ def get_remote_config_payload(
617617

618618
def get_all_flags_and_payloads(
619619
distinct_id,
620-
groups={},
621-
person_properties={},
622-
group_properties={},
620+
groups=None, # type: Optional[dict]
621+
person_properties=None, # type: Optional[dict]
622+
group_properties=None, # type: Optional[dict]
623623
only_evaluate_locally=False,
624624
disable_geoip=None, # type: Optional[bool]
625625
) -> FlagsAndPayloads:
626626
return _proxy(
627627
"get_all_flags_and_payloads",
628628
distinct_id=distinct_id,
629-
groups=groups,
630-
person_properties=person_properties,
631-
group_properties=group_properties,
629+
groups=groups or {},
630+
person_properties=person_properties or {},
631+
group_properties=group_properties or {},
632632
only_evaluate_locally=only_evaluate_locally,
633633
disable_geoip=disable_geoip,
634634
)

0 commit comments

Comments
 (0)