Skip to content

Commit 8737485

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 e6cdeb8 commit 8737485

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
@@ -245,9 +245,9 @@ def capture_exception(
245245
def feature_enabled(
246246
key, # type: str
247247
distinct_id, # type: str
248-
groups={}, # type: dict
249-
person_properties={}, # type: dict
250-
group_properties={}, # type: dict
248+
groups=None, # type: Optional[dict]
249+
person_properties=None, # type: Optional[dict]
250+
group_properties=None, # type: Optional[dict]
251251
only_evaluate_locally=False, # type: bool
252252
send_feature_flag_events=True, # type: bool
253253
disable_geoip=None, # type: Optional[bool]
@@ -270,9 +270,9 @@ def feature_enabled(
270270
"feature_enabled",
271271
key=key,
272272
distinct_id=distinct_id,
273-
groups=groups,
274-
person_properties=person_properties,
275-
group_properties=group_properties,
273+
groups=groups or {},
274+
person_properties=person_properties or {},
275+
group_properties=group_properties or {},
276276
only_evaluate_locally=only_evaluate_locally,
277277
send_feature_flag_events=send_feature_flag_events,
278278
disable_geoip=disable_geoip,
@@ -282,9 +282,9 @@ def feature_enabled(
282282
def get_feature_flag(
283283
key, # type: str
284284
distinct_id, # type: str
285-
groups={}, # type: dict
286-
person_properties={}, # type: dict
287-
group_properties={}, # type: dict
285+
groups=None, # type: Optional[dict]
286+
person_properties=None, # type: Optional[dict]
287+
group_properties=None, # type: Optional[dict]
288288
only_evaluate_locally=False, # type: bool
289289
send_feature_flag_events=True, # type: bool
290290
disable_geoip=None, # type: Optional[bool]
@@ -315,9 +315,9 @@ def get_feature_flag(
315315
"get_feature_flag",
316316
key=key,
317317
distinct_id=distinct_id,
318-
groups=groups,
319-
person_properties=person_properties,
320-
group_properties=group_properties,
318+
groups=groups or {},
319+
person_properties=person_properties or {},
320+
group_properties=group_properties or {},
321321
only_evaluate_locally=only_evaluate_locally,
322322
send_feature_flag_events=send_feature_flag_events,
323323
disable_geoip=disable_geoip,
@@ -326,9 +326,9 @@ def get_feature_flag(
326326

327327
def get_all_flags(
328328
distinct_id, # type: str
329-
groups={}, # type: dict
330-
person_properties={}, # type: dict
331-
group_properties={}, # type: dict
329+
groups=None, # type: Optional[dict]
330+
person_properties=None, # type: Optional[dict]
331+
group_properties=None, # type: Optional[dict]
332332
only_evaluate_locally=False, # type: bool
333333
disable_geoip=None, # type: Optional[bool]
334334
) -> Optional[dict[str, FeatureFlag]]:
@@ -344,9 +344,9 @@ def get_all_flags(
344344
return _proxy(
345345
"get_all_flags",
346346
distinct_id=distinct_id,
347-
groups=groups,
348-
person_properties=person_properties,
349-
group_properties=group_properties,
347+
groups=groups or {},
348+
person_properties=person_properties or {},
349+
group_properties=group_properties or {},
350350
only_evaluate_locally=only_evaluate_locally,
351351
disable_geoip=disable_geoip,
352352
)
@@ -355,23 +355,23 @@ def get_all_flags(
355355
def get_feature_flag_result(
356356
key,
357357
distinct_id,
358-
groups={},
359-
person_properties={},
360-
group_properties={},
358+
groups=None, # type: Optional[dict]
359+
person_properties=None, # type: Optional[dict]
360+
group_properties=None, # type: Optional[dict]
361361
only_evaluate_locally=False,
362362
send_feature_flag_events=True,
363363
disable_geoip=None, # type: Optional[bool]
364364
) -> Optional[FeatureFlagResult]: # type hint for feature flag result
365365
"""
366366
Get a FeatureFlagResult object which contains the flag result and payload.
367-
367+
368368
This method evaluates a feature flag and returns a FeatureFlagResult object containing:
369369
- enabled: Whether the flag is enabled
370370
- variant: The variant value if the flag has variants
371371
- payload: The payload associated with the flag (automatically deserialized from JSON)
372372
- key: The flag key
373373
- reason: Why the flag was enabled/disabled
374-
374+
375375
Example:
376376
```python
377377
result = posthog.get_feature_flag_result('beta-feature', 'distinct_id')
@@ -385,9 +385,9 @@ def get_feature_flag_result(
385385
"get_feature_flag_result",
386386
key=key,
387387
distinct_id=distinct_id,
388-
groups=groups,
389-
person_properties=person_properties,
390-
group_properties=group_properties,
388+
groups=groups or {},
389+
person_properties=person_properties or {},
390+
group_properties=group_properties or {},
391391
only_evaluate_locally=only_evaluate_locally,
392392
send_feature_flag_events=send_feature_flag_events,
393393
disable_geoip=disable_geoip,
@@ -398,9 +398,9 @@ def get_feature_flag_payload(
398398
key,
399399
distinct_id,
400400
match_value=None,
401-
groups={},
402-
person_properties={},
403-
group_properties={},
401+
groups=None, # type: Optional[dict]
402+
person_properties=None, # type: Optional[dict]
403+
group_properties=None, # type: Optional[dict]
404404
only_evaluate_locally=False,
405405
send_feature_flag_events=True,
406406
disable_geoip=None, # type: Optional[bool]
@@ -410,9 +410,9 @@ def get_feature_flag_payload(
410410
key=key,
411411
distinct_id=distinct_id,
412412
match_value=match_value,
413-
groups=groups,
414-
person_properties=person_properties,
415-
group_properties=group_properties,
413+
groups=groups or {},
414+
person_properties=person_properties or {},
415+
group_properties=group_properties or {},
416416
only_evaluate_locally=only_evaluate_locally,
417417
send_feature_flag_events=send_feature_flag_events,
418418
disable_geoip=disable_geoip,
@@ -441,18 +441,18 @@ def get_remote_config_payload(
441441

442442
def get_all_flags_and_payloads(
443443
distinct_id,
444-
groups={},
445-
person_properties={},
446-
group_properties={},
444+
groups=None, # type: Optional[dict]
445+
person_properties=None, # type: Optional[dict]
446+
group_properties=None, # type: Optional[dict]
447447
only_evaluate_locally=False,
448448
disable_geoip=None, # type: Optional[bool]
449449
) -> FlagsAndPayloads:
450450
return _proxy(
451451
"get_all_flags_and_payloads",
452452
distinct_id=distinct_id,
453-
groups=groups,
454-
person_properties=person_properties,
455-
group_properties=group_properties,
453+
groups=groups or {},
454+
person_properties=person_properties or {},
455+
group_properties=group_properties or {},
456456
only_evaluate_locally=only_evaluate_locally,
457457
disable_geoip=disable_geoip,
458458
)

0 commit comments

Comments
 (0)