hyrax.config_migrations#
Versioned migrations for Hyrax user configuration files.
Hyrax tags its config schema with a top-level config_version scalar in
hyrax_default_config.toml. When a user loads an older config, the
migrations registered here run before the merge step in
hyrax.config_utils.ConfigManager, bringing the user’s document
forward one version at a time until it matches CURRENT_CONFIG_VERSION.
Each migration step lives in its own descriptively-named module (e.g.
001_rename_model_inputs_to_data_request.py) and self-registers via the
migration_step() decorator, which populates the MIGRATIONS dict.
CURRENT_CONFIG_VERSION is auto-derived from the highest registered
migration — developers do not bump it manually.
Adding a new migration:
Create
src/hyrax/config_migrations/migrations/00N_description.py(e.g.002_move_learning_rate.py). Decorate the migration function with@migration_step(from_version=N, key_renames={...}). Import the decorator and helpers fromhyrax.config_migrations.migration_utils. The module is auto-discovered — no import line needed elsewhere.CURRENT_CONFIG_VERSIONandconfig_versionin the default TOML are both stamped automatically at runtime.Add a unit test in
tests/hyrax/test_config_migrations.py.
Submodules#
Attributes#
Classes#
A single schema migration with optional key-rename metadata. |
Functions#
|
Upgrade a user config document to the current schema version. |
|
Decorator that registers a migration function into |
|
Move a nested key from |
|
Rename a top-level table from |
Package Contents#
- MIGRATIONS: dict[int, MigrationStep][source]#
- class MigrationStep[source]#
A single schema migration with optional key-rename metadata.
- Parameters:
func (Callable[[TOMLDocument], TOMLDocument]) – The function that upgrades a config document by one version.
key_renames (dict[str, str]) – Old dotted path -> new dotted path for every key renamed by this migration. Used by
ConfigManager.set_configto warn callers who still reference the old names at runtime.
- func: collections.abc.Callable[[tomlkit.toml_document.TOMLDocument], tomlkit.toml_document.TOMLDocument]#
- key_renames: dict[str, str]#
- migrate_config(user_config: tomlkit.toml_document.TOMLDocument, *, _migrations: dict[int, MigrationStep] | None = None, _target_version: int | None = None) tomlkit.toml_document.TOMLDocument[source]#
Upgrade a user config document to the current schema version.
The document is mutated in place (and also returned for convenience). If
config_versionis absent it is assumed to be1— older configs predate the versioning field. If it is greater than the current schema version, aRuntimeErroris raised because the installed Hyrax does not know how to read the schema.- Parameters:
user_config (TOMLDocument) – The parsed user config. An empty document (the “no user config” case) is returned unchanged.
_migrations (dict[int, MigrationStep], optional) – Override the global
MIGRATIONSregistry (testing only)._target_version (int, optional) – Override the auto-derived target version (testing only).
- Returns:
The upgraded document with
config_versionstamped to the current value.- Return type:
TOMLDocument
- Raises:
RuntimeError – If
user_configdeclares a version newer than this Hyrax can understand, or if a migration step is missing fromMIGRATIONS.
- migration_step(from_version: int, key_renames: dict[str, str] | None = None)[source]#
Decorator that registers a migration function into
MIGRATIONS.- Parameters:
from_version (int) – The config schema version this function upgrades FROM. The target version is implicitly
from_version + 1.key_renames (dict[str, str], optional) – Old dotted path -> new dotted path for every key renamed by this migration.
- move_key(cfg: tomlkit.toml_document.TOMLDocument | dict, old_path: str, new_path: str) bool[source]#
Move a nested key from
old_pathtonew_path.Paths are dotted strings parsed by
hyrax.config_utils.parse_dotted_key(), so quoted segments like"'torch.optim.Adam'.lr"are supported.- Parameters:
cfg (TOMLDocument or dict) – The user config document to mutate in place.
old_path (str) – The dotted path to the existing key.
new_path (str) – The dotted path to write the value to. Intermediate tables are created as needed.
- Returns:
Trueiffold_pathwas present and the value was moved.- Return type:
bool
- rename_table(cfg: tomlkit.toml_document.TOMLDocument | dict, old: str, new: str) bool[source]#
Rename a top-level table from
oldtonew.If only
oldexists it is moved. If both exist,cfg[new]wins on leaf collisions; the two are deep-merged via ConfigManager.merge_configs andoldis removed. If neither exists this is a no-op.- Parameters:
cfg (TOMLDocument or dict) – The user config document to mutate in place.
old (str) – The table name that is being retired.
new (str) – The new table name to adopt.
- Returns:
Trueiffoldwas present and something was renamed (so the caller can emit a deprecation warning conditionally).- Return type:
bool