8000 feat: Add new FeatureFlagResult class and tests by haacked · Pull Request #227 · PostHog/posthog-python · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Add new FeatureFlagResult class and tests #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 24, 2025

Conversation

haacked
Copy link
Collaborator
@haacked haacked commented Apr 23, 2025

Added new method get_feature_flag_result which returns a FeatureFlagResult object. This object breaks down the result of a feature flag into its enabled state, variant, and payload. The benefit of this method is it allows you to retrieve the result of a feature flag and its payload in a single API call. You can call get_value on the result to get the value of the feature flag, which is the same value returned by get_feature_flag (aka the string variant if the flag is a multivariate flag or the boolean value if the flag is a boolean flag).

Example:

result = posthog.get_feature_flag_result("my-flag", "distinct_id")
print(result.enabled)     # True or False
print(result.variant)     # 'the-variant-value' or None
print(result.payload)     # {'foo': 'bar'}
print(result.get_value()) # 'the-variant-value' or True or False

🚨 Breaking change ⚠️ :

get_feature_flag_payload now deserializes payloads from JSON strings to Any. Previously, it returned the payload as a JSON encoded string.

Before:

payload = get_feature_flag_payload('key', 'distinct_id') # "{\"some\": \"payload\"}"

After:

payload = get_feature_flag_payload('key', 'distinct_id') # {"some": "payload"}

@haacked haacked requested review from Copilot and dmarticus April 23, 2025 17:44
Copy link
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new FeatureFlagResult class and the associated get_feature_flag_result method while updating the existing feature flag payload handling to return deserialized JSON objects. Key changes include:

  • Updating the version to 4.0.0 and adding the changelog for the new feature and breaking changes.
  • Creating the FeatureFlagResult dataclass with helper constructors and integrating it into the client’s feature flag retrieval flow.
  • Adjusting tests to reflect the new feature flag payload format and default behaviors.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
posthog/version.py Version bump to 4.0.0 to support the new feature.
posthog/types.py Added the FeatureFlagResult dataclass and related methods.
posthog/test/test_feature_flags.py Updated test cases to account for JSON payload changes and regex syntax.
posthog/test/test_feature_flag.py Adjusted default expected condition_index value as part of the breaking change.
posthog/client.py Refactored feature flag methods to use the new FeatureFlagResult and improved local/remote evaluation handling.
CHANGELOG.md Documented the new functionality and breaking changes in feature flag payloads.

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.

PR Summary

This PR introduces a new FeatureFlagResult class and get_feature_flag_result method to provide a unified way to access feature flag data, along with a breaking change in payload deserialization.

  • Added FeatureFlagResult class in /posthog/types.py with properties enabled, variant, payload and get_value() method
  • Breaking change: get_feature_flag_payload now deserializes JSON string payloads to Python objects instead of returning raw JSON strings
  • Duplicate test method name test_from_bool_value_and_payload in /posthog/test/test_feature_flag_result.py needs to be fixed
  • Missing test coverage for invalid JSON payloads and malformed flag definitions
  • Version bumped to 4.0.0 in /posthog/version.py to reflect breaking API change

7 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile

Copy link
Contributor
@dmarticus dmarticus left a comment

Choose a reason for hiding this comment

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

great as always, one question about what we decided to return in the FeatureFlagResponse but nothing blocking.

haacked and others added 13 commits April 24, 2025 08:46
This provides a "one-shot" method to get both the feature flag result and payload without having to make two decide calls.
Add private `_get_feature_flag_result` method

This will also handle the logic for `get_feature_flag_payload`
Now uses the new `_get_feature_flag_result` method.
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@haacked haacked force-pushed the haacked/feature-flag-details branch 2 times, most recently from 95196ad to f3740d5 Compare April 24, 2025 16:33
@haacked haacked force-pushed the haacked/feature-flag-details branch from 347390c to 27b0965 Compare April 24, 2025 16:50
@haacked haacked force-pushed the haacked/feature-flag-details branch from 27b0965 to de657b7 Compare April 24, 2025 16:53
@haacked haacked merged commit c1f668e into master Apr 24, 2025
6 checks passed
@haacked haacked deleted the haacked/feature-flag-details branch April 24, 2025 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0