@@ -157,39 +157,15 @@ class CompoundCachePolicy(CachePolicy):
157
157
158
158
policies : list [CachePolicy ] = field (default_factory = list )
159
159
160
- def __sub__ (self , other : str ) -> CachePolicy :
161
- if not isinstance (other , str ): # type: ignore[reportUnnecessaryIsInstance]
162
- raise TypeError ("Can only subtract strings from key policies." )
163
-
164
- # Subtract from each sub-policy; this may stack new Inputs(...) layers
165
- new_subpolicies : list [CachePolicy ] = [p - other for p in self .policies ]
166
-
167
- # If there are multiple Inputs, unify them all into one
168
- inputs_policies = [p for p in new_subpolicies if isinstance (p , Inputs )]
160
+ def __post_init__ (self ) -> None :
161
+ # deduplicate any Inputs policies
162
+ inputs_policies = [p for p in self .policies if isinstance (p , Inputs )]
163
+ self .policies = [p for p in self .policies if not isinstance (p , Inputs )]
169
164
if inputs_policies :
170
- # Gather all excludes into a single set
171
165
all_excludes : set [str ] = set ()
172
166
for inputs_policy in inputs_policies :
173
167
all_excludes .update (inputs_policy .exclude )
174
-
175
- # Remove all old Inputs from the subpolicy list
176
- new_subpolicies = [p for p in new_subpolicies if not isinstance (p , Inputs )]
177
-
178
- # Append one merged Inputs that excludes the union
179
- merged_inputs = Inputs (exclude = sorted (all_excludes ))
180
- new_subpolicies .append (merged_inputs )
181
-
182
- # If no sub‐policies exist after this, create an Inputs with [other]
183
- # (to preserve the "always add an Inputs" behavior)
184
- if not new_subpolicies :
185
- new_subpolicies = [Inputs (exclude = [other ])]
186
-
187
- return CompoundCachePolicy (
188
- policies = new_subpolicies ,
189
- key_storage = self .key_storage ,
190
- isolation_level = self .isolation_level ,
191
- lock_manager = self .lock_manager ,
192
- )
168
+ self .policies .append (Inputs (exclude = sorted (all_excludes )))
193
169
194
170
def compute_key (
195
171
self ,
@@ -212,6 +188,28 @@ def compute_key(
212
188
return None
213
189
return hash_objects (* keys , raise_on_failure = True )
214
190
191
+ def __add__ (self , other : "CachePolicy" ) -> "CachePolicy" :
192
+ # Call the superclass add method to handle validation
193
+ super ().__add__ (other )
194
+
195
+ if isinstance (other , CompoundCachePolicy ):
196
+ policies = [* self .policies , * other .policies ]
197
+ else :
198
+ policies = [* self .policies , other ]
199
+
200
+ return CompoundCachePolicy (
201
+ policies = policies ,
202
+ key_storage = self .key_storage or other .key_storage ,
203
+ isolation_level = self .isolation_level or other .isolation_level ,
204
+ lock_manager = self .lock_manager or other .lock_manager ,
205
+ )
206
+
207
+ def __sub__ (self , other : str ) -> "CachePolicy" :
208
+ if not isinstance (other , str ): # type: ignore[reportUnnecessaryIsInstance]
209
+ raise TypeError ("Can only subtract strings from key policies." )
210
+ new = Inputs (exclude = [other ])
211
+ return CompoundCachePolicy (policies = [* self .policies , new ])
212
+
215
213
216
214
@dataclass
217
215
class _None (CachePolicy ):
0 commit comments