3030from flagsmith .types import (
3131 ApplicationMetadata ,
3232 JsonType ,
33- SDKEvaluationContext ,
3433 StreamEvent ,
3534 TraitMapping ,
3635)
@@ -129,7 +128,6 @@ def __init__(
129128 self .enable_realtime_updates = enable_realtime_updates
130129 self ._analytics_processor : typing .Optional [AnalyticsProcessor ] = None
131130 self ._event_processor : typing .Optional [EventProcessor ] = None
132- self .__evaluation_context : typing .Optional [SDKEvaluationContext ] = None
133131 self ._local_evaluation : typing .Optional [LocalEvaluation ] = None
134132 self ._environment_updated_at : typing .Optional [datetime ] = None
135133
@@ -152,8 +150,10 @@ def __init__(
152150 )
153151
154152 if self .offline_handler :
155- self ._evaluation_context = map_environment_document_to_context (
156- self .offline_handler .get_environment ()
153+ self ._local_evaluation = LocalEvaluation (
154+ map_environment_document_to_context (
155+ self .offline_handler .get_environment ()
156+ )
157157 )
158158
159159 if not self .offline_mode :
@@ -242,12 +242,13 @@ def _initialise_local_evaluation(self) -> None:
242242 # method calls, update the environment manually.
243243 self .update_environment ()
244244 if self .enable_realtime_updates :
245- if not self ._evaluation_context :
245+ if not self ._local_evaluation :
246246 raise ValueError ("Unable to get environment from API key" )
247247
248+ environment_key = self ._local_evaluation .context ["environment" ]["key" ]
248249 stream_url = urljoin (
249250 self .realtime_api_url ,
250- f"sse/environments/{ self . _evaluation_context [ 'environment' ][ 'key' ] } /stream" ,
251+ f"sse/environments/{ environment_key } /stream" ,
251252 )
252253
253254 self .event_stream_thread = EventStreamManager (
@@ -285,7 +286,7 @@ def get_environment_flags(self) -> Flags:
285286 """
286287 if (
287288 self .offline_mode or self .enable_local_evaluation
288- ) and self ._evaluation_context :
289+ ) and self ._local_evaluation :
289290 return self ._get_environment_flags_from_document ()
290291 return self ._get_environment_flags_from_api ()
291292
@@ -313,7 +314,7 @@ def get_identity_flags(
313314 traits = traits or {}
314315 if (
315316 self .offline_mode or self .enable_local_evaluation
316- ) and self ._evaluation_context :
317+ ) and self ._local_evaluation :
317318 return self ._get_identity_flags_from_document (identifier , traits )
318319 return self ._get_identity_flags_from_api (
319320 identifier ,
@@ -343,7 +344,7 @@ def get_identity_segments(
343344 # Identity overrides segments are left out: they aren't segments
344345 # in Flagsmith, so they're never returned.
345346 context = map_context_and_identity_data_to_context (
346- context = local_evaluation .base_context ,
347+ context = local_evaluation .context ,
347348 identifier = identifier ,
348349 traits = traits ,
349350 )
@@ -466,8 +467,8 @@ def update_environment(self) -> None:
466467 logger .exception ("Error retrieving environment document from API" )
467468 else :
468469 try :
469- self ._evaluation_context = map_environment_document_to_context (
470- environment_data ,
470+ self ._local_evaluation = LocalEvaluation (
471+ map_environment_document_to_context ( environment_data )
471472 )
472473 self ._environment_updated_at = (
473474 map_environment_document_to_environment_updated_at (
@@ -477,25 +478,6 @@ def update_environment(self) -> None:
477478 except (KeyError , TypeError , ValueError ):
478479 logger .exception ("Error parsing environment document" )
479480
480- @property
481- def _evaluation_context (self ) -> typing .Optional [SDKEvaluationContext ]:
482- return self .__evaluation_context
483-
484- @_evaluation_context .setter
485- def _evaluation_context (
486- self , context : typing .Optional [SDKEvaluationContext ]
487- ) -> None :
488- """Swap in a new evaluation context and rebuild local evaluation state.
489-
490- `LocalEvaluation` holds everything derived from the environment
491- document, e.g. the segment overrides index. Rebuilt once per refresh
492- and reused across every evaluation, without any hot-path cost.
493- """
494- self ._local_evaluation = (
495- LocalEvaluation (context ) if context is not None else None
496- )
497- self .__evaluation_context = context
498-
499481 def _get_headers (
500482 self ,
501483 environment_key : str ,
0 commit comments