config
Main configuration management API.
Config
Configuration management with continuous validation, coercion, resolved references, and instantiation.
Main entry point for loading, managing, and resolving configurations. Supports YAML files with resolved references (@), raw references (%), expressions ($), and dynamic instantiation (target).
Example
from sparkwheel import Config
# Create and load from file
config = Config(schema=MySchema).update("config.yaml")
# Or chain multiple sources
config = (Config(schema=MySchema)
.update("base.yaml")
.update("override.yaml")
.update({"model::lr": 0.001}))
# Access raw values
lr = config.get("model::lr")
# Set values (validates automatically if schema provided)
config.set("model::dropout", 0.1)
# Freeze to prevent modifications
config.freeze()
# Resolve references and instantiate
model = config.resolve("model")
everything = config.resolve()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
globals
|
dict[str, Any] | None
|
Pre-imported packages for expressions (e.g., {"torch": "torch"}) |
None
|
schema
|
type | None
|
Dataclass schema for continuous validation |
None
|
coerce
|
bool
|
Auto-convert compatible types (default: True) |
True
|
strict
|
bool
|
Reject fields not in schema (default: True) |
True
|
allow_missing
|
bool
|
Allow MISSING sentinel values (default: False) |
False
|
Source code in src/sparkwheel/config.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | |
__contains__(id)
Check if ID exists in config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
ID path to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if exists, False otherwise |
Source code in src/sparkwheel/config.py
__getitem__(id)
Get config value by ID (subscript access).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Configuration path |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Config value at that path |
Example
config = Config.load({"model": {"lr": 0.001}}) config["model::lr"] 0.001
Source code in src/sparkwheel/config.py
__init__(data=None, *, globals=None, schema=None, coerce=True, strict=True, allow_missing=False)
Initialize Config container.
Normally starts empty - use update() to load data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any] | None
|
Initial data (internal/testing use only, not validated) |
None
|
globals
|
dict[str, Any] | None
|
Pre-imported packages for expression evaluation |
None
|
schema
|
type | None
|
Dataclass schema for continuous validation |
None
|
coerce
|
bool
|
Auto-convert compatible types |
True
|
strict
|
bool
|
Reject fields not in schema |
True
|
allow_missing
|
bool
|
Allow MISSING sentinel values |
False
|
Examples:
Source code in src/sparkwheel/config.py
__repr__()
__setitem__(id, value)
Set config value by ID (subscript access).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Configuration path |
required |
value
|
Any
|
Value to set |
required |
Example
config = Config.load({}) config["model::lr"] = 0.001
Source code in src/sparkwheel/config.py
_apply_path_updates(source)
Apply nested path updates (e.g., model::lr=value, =model=replace, ~old::param=null).
Source code in src/sparkwheel/config.py
_apply_structural_update(source)
Apply structural update with operators.
_delete_nested_key(key)
Delete a key, supporting nested paths with ::.
Source code in src/sparkwheel/config.py
_get_by_id(id)
Get config value by ID path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
ID path (e.g., "model::lr") |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Config value at that path |
Raises:
| Type | Description |
|---|---|
KeyError
|
If path not found |
Source code in src/sparkwheel/config.py
_invalidate_resolution()
_parse(reset=True)
Parse config tree and prepare for resolution.
Internal method called automatically by resolve().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reset
|
bool
|
Whether to reset the resolver before parsing (default: True) |
True
|
Source code in src/sparkwheel/config.py
_update_from_config(source)
Update from another Config instance.
_update_from_file(source)
Load and update from a file.
Source code in src/sparkwheel/config.py
_update_from_override_string(override)
Parse and apply a single override string (e.g., 'key=value', '=key=value', '~key').
_uses_nested_paths(source)
export_config_file(config, filepath, **kwargs)
staticmethod
Export config to YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Config dict to export |
required |
filepath
|
PathLike
|
Target file path |
required |
kwargs
|
Any
|
Additional arguments for yaml.safe_dump |
{}
|
Source code in src/sparkwheel/config.py
freeze()
Freeze config to prevent further modifications.
After freezing: - set() raises FrozenConfigError - update() raises FrozenConfigError - resolve() still works (read-only) - get() still works (read-only)
Example
config = Config(schema=MySchema).update("config.yaml") config.freeze() config.set("model::lr", 0.001) # Raises FrozenConfigError
Source code in src/sparkwheel/config.py
get(id='', default=None)
Get raw config value (unresolved).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Configuration path (use :: for nesting, e.g., "model::lr") Empty string returns entire config |
''
|
default
|
Any
|
Default value if id not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Raw configuration value (resolved references not resolved, raw references not expanded) |
Example
config = Config.load({"model": {"lr": 0.001, "ref": "@model::lr"}}) config.get("model::lr") 0.001 config.get("model::ref") "@model::lr" # Unresolved resolved reference
Source code in src/sparkwheel/config.py
is_frozen()
resolve(id='', instantiate=True, eval_expr=True, lazy=True, default=None)
Resolve resolved references (@) and return parsed config.
Automatically parses config on first call. Resolves @ resolved references (follows them to get instantiated/evaluated values), evaluates $ expressions, and instantiates target components. Note: % raw references are expanded during preprocessing (before this stage).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Config path to resolve (empty string for entire config) |
''
|
instantiate
|
bool
|
Whether to instantiate components with target |
True
|
eval_expr
|
bool
|
Whether to evaluate $ expressions |
True
|
lazy
|
bool
|
Whether to use cached resolution |
True
|
default
|
Any
|
Default value if id not found (returns default.get_config() if Item) |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Resolved value (instantiated objects, evaluated expressions, etc.) |
Example
config = Config.load({ ... "lr": 0.001, ... "doubled": "$@lr * 2", ... "optimizer": { ... "target": "torch.optim.Adam", ... "lr": "@lr" ... } ... }) config.resolve("lr") 0.001 config.resolve("doubled") 0.002 optimizer = config.resolve("optimizer") type(optimizer).name 'Adam'
Source code in src/sparkwheel/config.py
set(id, value)
Set config value, creating paths as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Configuration path (use :: for nesting) |
required |
value
|
Any
|
Value to set |
required |
Raises:
| Type | Description |
|---|---|
FrozenConfigError
|
If config is frozen |
Example
config = Config() config.set("model::lr", 0.001) config.get("model::lr") 0.001
Source code in src/sparkwheel/config.py
unfreeze()
update(source)
Update configuration with changes from another source.
Auto-detects strings as either file paths or CLI overrides: - Strings with '=' are parsed as overrides (e.g., "key=value", "=key=value", "~key") - Strings without '=' are treated as file paths - Dicts and Config instances work as before
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
PathLike | dict[str, Any] | Config | str
|
File path, override string, dict, or Config instance to update from |
required |
Returns:
| Type | Description |
|---|---|
Config
|
self (for chaining) |
Operators
- key=value - Compose (default): merge dict or extend list
- =key=value - Replace operator: completely replace value
- ~key - Remove operator: delete key (idempotent)
Examples:
>>> # Chain multiple updates (mixed files and overrides)
>>> config = (Config(schema=MySchema)
... .update("base.yaml")
... .update("exp.yaml")
... .update("optimizer::lr=0.01")
... .update("=model={'_target_': 'MyModel'}")
... .update("~debug"))
>>> # Update from another Config instance
>>> config1 = Config()
>>> config2 = Config().update({"model::lr": 0.001})
>>> config1.update(config2)
Source code in src/sparkwheel/config.py
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 | |
validate(schema)
Validate configuration against a dataclass schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
type
|
Dataclass type defining the expected structure and types |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If configuration doesn't match schema |
TypeError
|
If schema is not a dataclass |
Example
from dataclasses import dataclass @dataclass ... class ModelConfig: ... hidden_size: int ... dropout: float config = Config.load({"hidden_size": 512, "dropout": 0.1}) config.validate(ModelConfig) # Passes bad_config = Config.load({"hidden_size": "not an int"}) bad_config.validate(ModelConfig) # Raises ValidationError
Source code in src/sparkwheel/config.py
parse_overrides(args)
Parse CLI argument overrides with automatic type inference.
Supports only key=value syntax with operator prefixes. Types are automatically inferred using ast.literal_eval().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[str]
|
List of argument strings to parse (e.g., from argparse) |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary of parsed key-value pairs with inferred types. |
dict[str, Any]
|
Keys may have operator prefixes (=key for replace, ~key for delete). |
Operators
- key=value - Normal assignment (composes/merges)
- =key=value - Replace operator (completely replaces key)
- ~key - Delete operator (removes key)
Examples:
>>> # Basic overrides (compose/merge)
>>> parse_overrides(["model::lr=0.001", "debug=True"])
{"model::lr": 0.001, "debug": True}
>>> # With operators
>>> parse_overrides(["=model={'_target_': 'ResNet'}", "~old_param"])
{"=model": {'_target_': 'ResNet'}, "~old_param": None}
>>> # Nested paths with operators
>>> parse_overrides(["=optimizer::lr=0.01", "~model::old_param"])
{"=optimizer::lr": 0.01, "~model::old_param": None}
Note
The '=' character serves dual purpose: - In 'key=value' → assignment operator (CLI syntax) - In '=key=value' → replace operator prefix (config operator)