:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(flags): accept a number for the numeric comparison operators (#100268)
Patricioac718b423d5e
diff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.pyindex 7fe572627963..a6643524ee98 100644--- a/products/feature_flags/backend/api/test/test_feature_flag.py+++ b/products/feature_flags/backend/api/test/test_feature_flag.py@@ -225,18 +225,11 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):def test_cant_create_flag_with_invalid_filters(self):count = FeatureFlag.objects.count()- invalid_operators = [- "icontains",- "regex",- "not_icontains",- "not_regex",- "lt",- "gt",- "lte",- "gte",- ]+ string_only_operators = ["icontains", "regex", "not_icontains", "not_regex"]+ numeric_operators = ["lt", "gt", "lte", "gte"]- for operator in invalid_operators:+ for operator in string_only_operators + numeric_operators:+ expected_kinds = "a string or number" if operator in numeric_operators else "a string"response = self.client.post(f"/api/projects/{self.team.id}/feature_flags",{@@ -265,7 +258,7 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):{"type": "validation_error","code": "cross_field.operator_requires_string_value",- "detail": f"groups[0].properties[0].value: Operator {operator} requires a string value.",+ "detail": f"groups[0].properties[0].value: Operator {operator} requires {expected_kinds} value.","attr": "filters",},)diff --git a/products/feature_flags/backend/filters_validation.py b/products/feature_flags/backend/filters_validation.pyindex 36301cb080fc..581e39374577 100644--- a/products/feature_flags/backend/filters_validation.py+++ b/products/feature_flags/backend/filters_validation.py@@ -14,6 +14,7 @@ reports or raises depending on the enforcement switch."""import re+import mathfrom collections.abc import Callable, Mappingfrom dataclasses import dataclassfrom typing import Any@@ -35,13 +36,27 @@ STRING_VALUE_OPERATORS: frozenset[str] = frozenset("not_regex","icontains","not_icontains",- "gt",- "gte",- "lt",- "lte",}| set(STRING_PREFIX_SUFFIX_OPERATORS))+# Every evaluator parses either form to the same number, so both are accepted.+NUMERIC_COMPARISON_OPERATORS: frozenset[str] = frozenset({"gt", "gte", "lt", "lte"})+++def _is_comparable_number(value: Any) -> bool:+ """Whether a number is one the Rust evaluator can read.++ It decodes filters with serde_json, which rejects a value outside f64 range with+ "number out of range" and fails the whole payload, not the one condition.+ """+ if isinstance(value, bool) or not isinstance(value, int | float):+ return False+ try:+ return math.isfinite(float(value))+ except OverflowError:+ return False++LIST_VALUE_OPERATORS: frozenset[str] = frozenset({"icontains_multi", "not_icontains_multi"})SEMVER_OPERATORS: frozenset[str] = frozenset({@@ -248,6 +263,16 @@ def check_operator_value_compatibility(filters: Mapping[str, Any]) -> list[Violamessage=f"Operator {operator} requires a string value.",))+ if operator in NUMERIC_COMPARISON_OPERATORS and not (+ isinstance(value, str) or _is_comparable_number(value)+ ):+ violations.append(+ Violation(+ rule_id="cross_field.operator_requires_string_value",+ path=path,+ message=f"Operator {operator} requires a string or number value.",+ )+ )if operator in LIST_VALUE_OPERATORS and not isinstance(value, list):violations.append(Violation(diff --git a/products/feature_flags/backend/test/test_filters_validation.py b/products/feature_flags/backend/test/test_filters_validation.pyindex 6539b26bd551..30ffe4b5371e 100644--- a/products/feature_flags/backend/test/test_filters_validation.py+++ b/products/feature_flags/backend/test/test_filters_validation.py@@ -163,6 +163,36 @@ class TestFiltersValidation(SimpleTestCase):("gt_operator_numeric_value",{"groups": [{"properties": [_person_prop(operator="gt", value=5)]}]},+ [],+ ),+ (+ "gte_operator_float_value",+ {"groups": [{"properties": [_person_prop(operator="gte", value=1.5)]}]},+ [],+ ),+ (+ "lt_operator_string_value",+ {"groups": [{"properties": [_person_prop(operator="lt", value="5")]}]},+ [],+ ),+ (+ "lte_operator_boolean_value",+ {"groups": [{"properties": [_person_prop(operator="lte", value=True)]}]},+ ["cross_field.operator_requires_string_value"],+ ),+ (+ "gte_operator_value_over_f64_range",+ {"groups": [{"properties": [_person_prop(operator="gte", value=10**400)]}]},+ ["cross_field.operator_requires_string_value"],+ ),+ (+ "gt_operator_list_value",+ {"groups": [{"properties": [_person_prop(operator="gt", value=[5])]}]},+ ["cross_field.operator_requires_string_value"],+ ),+ (+ "icontains_operator_numeric_value",+ {"groups": [{"properties": [_person_prop(operator="icontains", value=5)]}]},["cross_field.operator_requires_string_value"],),(