Skip to content

Commit df282a6

Browse files
authored
fix(flags): stringify regex properties as compact JSON
Local regex and not_regex matching called str() on the property.
1 parent dca2207 commit df282a6

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
posthog: patch
3+
---
4+
5+
Stringify non-string properties as compact JSON for local `regex` and `not_regex` matching, so booleans, lists, and objects match the same way they do on the flags service.

‎posthog/feature_flags.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,9 @@ def compute_exact_match(value, override_value):
12641264
return matched
12651265

12661266
if operator in ("regex", "not_regex"):
1267-
property_string = "null" if override_value is None else str(override_value)
1267+
property_string = _value_to_string(
1268+
override_value, UnresolvedFlagReason.MISSING_CONTEXT
1269+
)
12681270
regex_match = _match_regex(str(value), property_string)
12691271
if regex_match is None:
12701272
return False

‎posthog/test/test_feature_flags.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5871,6 +5871,25 @@ def test_match_properties_regex(self):
58715871

58725872
self.assertFalse(match_property(property_d, {"key": "value"}))
58735873

5874+
@parameterized.expand(
5875+
[
5876+
("list", r"\[1,2\]", [1, 2], True),
5877+
("bool", "^true$", True, True),
5878+
("python_bool_spelling", "True", True, False),
5879+
("object", r'^\{"a":1,"b":\[null\]\}$', {"b": [None], "a": 1}, True),
5880+
("python_object_spelling", "'a'", {"a": 1}, False),
5881+
]
5882+
)
5883+
def test_regex_stringifies_non_string_properties_as_compact_json(
5884+
self, _name, pattern, property_value, expected
5885+
):
5886+
regex = self.property("key", pattern, "regex")
5887+
not_regex = self.property("key", pattern, "not_regex")
5888+
self.assertEqual(expected, match_property(regex, {"key": property_value}))
5889+
self.assertEqual(
5890+
not expected, match_property(not_regex, {"key": property_value})
5891+
)
5892+
58745893
def test_match_properties_math_operators(self):
58755894
property_a = self.property(key="key", value=1, operator="gt")
58765895
self.assertTrue(match_property(property_a, {"key": 2}))

0 commit comments

Comments
 (0)