Skip to content

Conversation

@dmarticus
Copy link
Contributor

Fix: Return FeatureFlagResult for empty string payloads

Problem

When a feature flag variant has an empty string ("") as its payload, get_feature_flag_result() returns None instead of a FeatureFlagResult object with the empty string payload.

Solution

Updated falsy checks throughout the codebase to use explicit is not None checks instead of relying on Python's truthiness. This ensures empty strings are properly handled as valid payloads.

Changes

  • Fixed payload handling in client.py to check is not None instead of truthiness
  • Updated types.py to avoid JSON parsing empty strings
  • Added comprehensive tests for empty string payload scenarios

Testing

Added tests to verify:

  • get_feature_flag_result() returns a proper FeatureFlagResult with empty string payload
  • get_all_flags_and_payloads() includes flags with empty string payloads

Closes #296

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR fixes a bug in the PostHog Python SDK where feature flags with empty string payloads ("") were incorrectly being treated as falsy values, causing get_feature_flag_result() to return None instead of a proper FeatureFlagResult object. The issue stemmed from using Python's truthiness evaluation (if payload) instead of explicit null checks (if payload is not None) throughout the codebase.

The changes update three key areas: (1) In client.py, payload evaluation logic in _get_feature_flag_result, _capture_feature_flag_called, and _get_all_flags_and_payloads_locally now use explicit is not None checks, (2) In types.py, the FeatureFlag.from_value_and_payload() method removes the falsy check that was converting empty strings to None, and JSON parsing in FeatureFlagResult methods now checks for non-empty strings before attempting to parse, and (3) The to_payloads() method changes from falsy checks to explicit None checks to include empty string payloads in returned dictionaries.

This fix ensures that empty strings are treated as valid payloads throughout the feature flag evaluation pipeline, maintaining the expected contract that get_feature_flag_result() should return a FeatureFlagResult object when a flag matches, regardless of payload content. The changes are accompanied by comprehensive tests verifying both individual flag evaluation and bulk flag operations handle empty string payloads correctly. The version has been bumped to 6.3.3 to reflect this patch-level bug fix.

Confidence score: 5/5

  • This PR is safe to merge with minimal risk as it fixes a clear bug with well-defined behavior changes
  • Score reflects thorough testing coverage, clear problem identification, and surgical fixes that only affect the specific edge case
  • No files require special attention as the changes are straightforward and well-tested

5 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

@dmarticus dmarticus requested a review from a team August 2, 2025 00:27
@dmarticus dmarticus moved this to In Review in Feature Flags Aug 2, 2025
Copy link
Collaborator

@haacked haacked left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If null is a billion dollar mistake, "truthiness" is a 50-million dollar mistake. Nice fix!

@github-project-automation github-project-automation bot moved this from In Review to Approved in Feature Flags Aug 2, 2025
@dmarticus dmarticus merged commit 09dad81 into master Aug 2, 2025
11 checks passed
@dmarticus dmarticus deleted the fix/296 branch August 2, 2025 00:33
@github-project-automation github-project-automation bot moved this from Approved to Done in Feature Flags Aug 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

get_feature_flag_result returns None instead of FeatureFlagResult when payload is ""

3 participants