@@ -119,40 +119,18 @@ def _add_entry_at_level(
119119 _ensure_state (context )
120120 if level not in _LEVELS :
121121 raise ValueError (f"Unknown level: { level !r} " )
122+
123+ new_ctx = EvaluationContext (attributes = {key : value })
122124 if level == "API" :
123- current = api .get_evaluation_context ()
124- api .set_evaluation_context (
125- EvaluationContext (
126- targeting_key = current .targeting_key ,
127- attributes = {** current .attributes , key : value },
128- )
129- )
125+ api .set_evaluation_context (api .get_evaluation_context ().merge (new_ctx ))
130126 elif level == "Transaction" :
131- current = api .get_transaction_context ()
132- set_transaction_context (
133- EvaluationContext (
134- targeting_key = current .targeting_key ,
135- attributes = {** current .attributes , key : value },
136- )
137- )
127+ set_transaction_context (api .get_transaction_context ().merge (new_ctx ))
138128 elif level == "Client" :
139- current = context .client .context
140- context .client .context = EvaluationContext (
141- targeting_key = current .targeting_key ,
142- attributes = {** current .attributes , key : value },
143- )
129+ context .client .context = context .client .context .merge (new_ctx )
144130 elif level == "Invocation" :
145- current = context .invocation_context
146- context .invocation_context = EvaluationContext (
147- targeting_key = current .targeting_key ,
148- attributes = {** current .attributes , key : value },
149- )
131+ context .invocation_context = context .invocation_context .merge (new_ctx )
150132 elif level == "Before Hooks" :
151- current = context .before_hook_context
152- context .before_hook_context = EvaluationContext (
153- targeting_key = current .targeting_key ,
154- attributes = {** current .attributes , key : value },
155- )
133+ context .before_hook_context = context .before_hook_context .merge (new_ctx )
156134
157135
158136@given (
@@ -181,7 +159,9 @@ def step_impl_entries_down_to(
181159 context : typing .Any , level : str , key : str , value : str
182160) -> None :
183161 _ensure_state (context )
184- levels = context .precedence_levels
162+ levels = getattr (context , "precedence_levels" , None )
163+ if not levels :
164+ raise ValueError ("Precedence levels table has not been initialized. Ensure 'A table with levels of increasing precedence' step is run first." )
185165 if level not in levels :
186166 raise ValueError (f"Level { level !r} not in precedence table { levels !r} " )
187167 for current_level in levels :
@@ -205,7 +185,9 @@ def step_impl_evaluate(context: typing.Any) -> None:
205185
206186@then ('The merged context contains an entry with key "{key}" and value "{value}"' )
207187def step_impl_merged_contains (context : typing .Any , key : str , value : str ) -> None :
208- assert context .provider .last_context is not None , (
188+ provider = getattr (context , "provider" , None )
189+ assert provider is not None , "Provider is not initialized in context"
190+ assert provider .last_context is not None , (
209191 "provider did not receive an evaluation context"
210192 )
211193 attributes = context .provider .last_context .attributes
0 commit comments