I have a simple Typer application:
import typer
app = typer.Typer()
@app.command()
def say_hi():
print("Hi")
@app.callback()
def main():
pass
if __name__ == "__main__":
app()
I would like to use Hydra for the configuration management of the app, however I am not sure how to do that without losing the ability to override the config from the CLI.
My first attempt was:
import hydra
import typer
from omegaconf import DictConfig, OmegaConf
app = typer.Typer()
@app.command()
def say_hi():
print("Hi")
@app.callback()
@hydra.main(config_path="conf", config_name="config")
def main(cfg: DictConfig) -> None:
print(OmegaConf.to_yaml(cfg))
if __name__ == "__main__":
app()
But I get an error saying:
RuntimeError: Type not yet supported: <class 'omegaconf.dictconfig.DictConfig'>
If I remove the DictConfig
type annotation I get an error that cfg
is missing.
I saw in Hydra docs the Compose API that allows to initialize the config without decorators:
@app.callback()
def main() -> None:
with initialize(config_path="conf", job_name="test_app"):
cfg = compose(config_name="config")
print(OmegaConf.to_yaml(cfg))
but It seems that I can't override config from the command line in this case as those values are not recognized by the Typer app.
Any recommendations how it can be resolved?