'Can I get access to the global configs from an arbitrary function in my code?

I apologize in advance if what I'm trying to do is an anti-pattern. 😅

I would like to access my global configuration from somewhere in my code where it would be extremely cumbersome to pass the config object that the hydra-decorated main function uses.
I know that this is possible with the hydra-specific configs, using the HydraConfig object. Is there a similar construct for the application-specific configs? Thanks!



Solution 1:[1]

Is there a similar construct for the application-specific configs?

Nope, there is no such construct.

If you need access to global state, why not use a global variable?

# app.py
from typing import Optional

import hydra
from omegaconf import DictConfig

# global state
app_cfg: Optional[DictConfig] = None

def nested():
    global app_cfg
    assert app_cfg is not None
    print(f"{app_cfg.foo=}")

def fn():
    nested()

@hydra.main(config_path=None)
def app(cfg: DictConfig) -> None:
    global app_cfg
    app_cfg = cfg
    fn()

if __name__ == "__main__":
    app()
$ python app.py +foo=bar
app_cfg.foo='bar'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jasha