44import typing
55import warnings
66from contextlib import suppress
7- from functools import lru_cache , wraps
7+ from functools import lru_cache , partial , wraps
88
99import jsonpath_rfc9535
1010import semver
2121from flag_engine .result .types import EvaluationResult , FlagResult , SegmentResult
2222from flag_engine .segments import constants
2323from flag_engine .segments .types import ConditionOperator , ContextValue , is_context_value
24- from flag_engine .segments .utils import escape_double_quotes , get_matching_function
24+ from flag_engine .segments .utils import get_matching_function
2525from flag_engine .utils .hashing import get_hashed_percentage_for_object_ids
2626from flag_engine .utils .semver import is_semver
2727from flag_engine .utils .types import SupportsStr , get_casting_function
@@ -350,6 +350,16 @@ def inner(
350350}
351351
352352
353+ def _get_trait_value (
354+ context : EvaluationContext ,
355+ trait_key : str ,
356+ ) -> ContextValue :
357+ if identity_context := context .get ("identity" ):
358+ if traits := identity_context .get ("traits" ):
359+ return traits .get (trait_key )
360+ return None
361+
362+
353363@lru_cache
354364def _get_context_value_getter (
355365 property : str ,
@@ -366,11 +376,11 @@ def _get_context_value_getter(
366376 except jsonpath_rfc9535 .JSONPathSyntaxError :
367377 # This covers a rare case when a trait starting with "$.",
368378 # but not a valid JSONPath, is used.
369- compiled_query = jsonpath_rfc9535 .compile (
370- f'$.identity.traits["{ escape_double_quotes (property )} "]' ,
371- )
379+ return partial (_get_trait_value , trait_key = property )
372380
373381 def getter (context : EvaluationContext ) -> ContextValue :
382+ if trait_value := _get_trait_value (context , property ):
383+ return trait_value
374384 if typing .TYPE_CHECKING : # pragma: no cover
375385 # Ugly hack to satisfy mypy :(
376386 data = dict (context )
0 commit comments