operators
Configuration merging with composition-by-default and operators (=, ~).
MergeContext
dataclass
Context for configuration merging operations.
This class consolidates location tracking and path information needed during recursive config merging. Using a context object reduces parameter threading and makes it easier to extend with additional context in the future.
Attributes:
| Name | Type | Description |
|---|---|---|
locations |
LocationRegistry | None
|
Optional registry tracking file locations of config keys |
current_path |
str
|
Current path in config tree using :: separator (e.g., "model::optimizer::lr") |
Examples:
>>> ctx = MergeContext()
>>> child_ctx = ctx.child_path("model")
>>> child_ctx.current_path
'model'
>>> grandchild_ctx = child_ctx.child_path("lr")
>>> grandchild_ctx.current_path
'model::lr'
Source code in src/sparkwheel/operators.py
child_path(key)
Create a child context for a nested config key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key to append to current path |
required |
Returns:
| Type | Description |
|---|---|
MergeContext
|
New MergeContext with updated path, sharing the same source location registry |
Examples:
>>> ctx = MergeContext(current_path="model")
>>> child = ctx.child_path("optimizer")
>>> child.current_path
'model::optimizer'
Source code in src/sparkwheel/operators.py
get_source_location(key)
Get source location for a key in the current context.
Tries both the exact key and the key without operator prefix to handle operator keys correctly (e.g., ~key, =key).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key to look up (may include operators like ~key or =key) |
required |
Returns:
| Type | Description |
|---|---|
Location | None
|
Location if found, None otherwise |
Examples:
>>> ctx = MergeContext(locations=registry, current_path="model")
>>> ctx.get_source_location("~lr") # Looks up both "model::~lr" and "model::lr"
>>> ctx.get_source_location("=lr") # Looks up both "model::=lr" and "model::lr"
Source code in src/sparkwheel/operators.py
_validate_delete_operator(key, value)
Validate remove operator value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key name (without ~ prefix) |
required |
value
|
Any
|
The value provided with ~key |
required |
Raises:
| Type | Description |
|---|---|
ConfigMergeError
|
If value is not null, empty, or a list |
Source code in src/sparkwheel/operators.py
apply_operators(base, override, context=None)
Apply configuration changes with composition-by-default semantics.
Default behavior: Compose (merge dicts, extend lists) Operators: =key: value - Replace operator: completely replace value (override default) ~key: null - Remove operator: delete key or list items (errors if missing) key: value - Compose (default): merge dict or extend list
Composition-by-Default Philosophy
- Dicts merge recursively by default (preserves existing keys)
- Lists extend by default (append new items)
- Only scalars and type mismatches replace
- Use = to explicitly replace entire dicts or lists
- Use ~ to delete keys (errors if key doesn't exist)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
dict[str, Any]
|
Base configuration dict |
required |
override
|
dict[str, Any]
|
Override configuration dict with optional =/~ operators |
required |
context
|
MergeContext | None
|
Optional merge context with metadata and path tracking |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Merged configuration dict |
Raises:
| Type | Description |
|---|---|
ConfigMergeError
|
If operators are used incorrectly |
Examples:
>>> # Default: Dicts merge
>>> base = {"a": 1, "b": {"x": 1, "y": 2}}
>>> override = {"b": {"x": 10}}
>>> apply_operators(base, override)
{"a": 1, "b": {"x": 10, "y": 2}}
>>> # Default: Lists extend
>>> base = {"plugins": ["logger", "metrics"]}
>>> override = {"plugins": ["cache"]}
>>> apply_operators(base, override)
{"plugins": ["logger", "metrics", "cache"]}
>>> # Replace operator: explicit override
>>> base = {"model": {"lr": 0.001, "dropout": 0.1}}
>>> override = {"=model": {"lr": 0.01}}
>>> apply_operators(base, override)
{"model": {"lr": 0.01}}
>>> # Remove operator: delete key
>>> base = {"a": 1, "b": 2, "c": 3}
>>> override = {"b": 5, "~c": None}
>>> apply_operators(base, override)
{"a": 1, "b": 5}
>>> # With context for better error messages
>>> from .locations import LocationRegistry
>>> ctx = MergeContext(locations=LocationRegistry())
>>> apply_operators(base, override, context=ctx)
Source code in src/sparkwheel/operators.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
validate_operators(config, parent_key='')
Validate operator usage in config tree.
With composition-by-default, validation is simpler: 1. Remove operators error if key doesn't exist 2. Replace operators work on any type 3. No parent context requirements
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Configuration dict to validate |
required |
parent_key
|
str
|
Parent key path (for error messages) |
''
|
Raises:
| Type | Description |
|---|---|
ConfigMergeError
|
If operator usage is invalid |