Skip to content

Commit

Permalink
Add example compose callback and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jesszzzz committed Feb 5, 2025
1 parent 628a312 commit 4d82f56
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
age: 7
name: James Bond

hydra:
job:
name: test
callbacks:
log_compose:
_target_: hydra.experimental.callbacks.LogComposeCallback

defaults:
- config_schema
- _self_
- group: a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: a
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from dataclasses import dataclass
from typing import Dict

from omegaconf import MISSING

import hydra
from hydra.core.config_store import ConfigStore
from hydra.core.hydra_config import HydraConfig


@dataclass
class Config:
age: int = MISSING
name: str = MISSING
group: Dict[str, str] = MISSING


ConfigStore.instance().store(name="config_schema", node=Config)
ConfigStore.instance().store(name="config_schema", node=Config, group="test")


@hydra.main(version_base=None, config_path=".", config_name="config")
def my_app(cfg: Config) -> None:
print(
f"job_name: {HydraConfig().get().job.name}, "
f"name: {cfg.name}, age: {cfg.age}, group: {cfg.group['name']}"
)


if __name__ == "__main__":
my_app()
22 changes: 22 additions & 0 deletions tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@
r"\[JOB\] on_job_start task_function: <function my_app at 0x[0-9a-fA-F]+>",
id="on_job_start_task_function",
),
param(
"tests/test_apps/app_with_callbacks/app_with_log_compose_callback/my_app.py",
["age=10"],
dedent(
"""\
[HYDRA]
====
Composed config .*tests/test_apps/app_with_callbacks/app_with_log_compose_callback/config
age: 10
name: James Bond
group:
name: a
----
Includes overrides \\[.*'age=10'.*\\]
Used defaults \\['config_schema', 'config', 'group/a'\\]
====
job_name: test, name: James Bond, age: 10, group: a
"""
),
id="on_compose_callback",
),
],
)
def test_app_with_callbacks(
Expand Down

0 comments on commit 4d82f56

Please sign in to comment.