-
Sorry, another one… in my program I add validation like so: cs = ConfigStore.instance()
cs.store(name="config_schema", node=MyConfig)
@hydra.main(config_path="../conf", config_name="config", version_base=None)
def main(cfg: DictConfig) -> None:
cfg_instance = OmegaConf.to_object(cfg) The corresponding defaults:
- config_schema
- ... This works. Now I want to read the hydra config in @pytest.fixture
def hydra_config():
# Not sure if the context is the right approach here; or the method: don't know how often fixtures are called
with initialize(version_base=None, config_path="../src/conf", job_name="test_data"):
cfg = compose(config_name="config")
return cfg I get this error: So, I guess the question is: What is the “equivalent” of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
If your call to # my_configs.py
...
cs = ConfigStore.instance()
cs.store(name="config_schema", node=MyConfig)
... # test_my_configs.py
...
@pytest.fixture
def hydra_config():
import my_configs # to make sure the cs.store calls are executed
with initialize(version_base=None, config_path="../src/conf", job_name="test_data"):
cfg = compose(config_name="config")
return cfg Another approach is to put all of your calls to # my_configs.py
...
def store_configs():
cs = ConfigStore.instance()
cs.store(name="config_schema", node=MyConfig)
... # test_my_configs.py
from my_configs import store_configs
...
@pytest.fixture
def hydra_config():
store_configs() # execute cs.store calls
with initialize(version_base=None, config_path="../src/conf", job_name="test_data"):
cfg = compose(config_name="config")
return cfg # my_app.py
from my_configs import store_configs
...
@hydra.main(...)
def run_app(cfg):
...
if __name__ == "__main__":
store_configs()
run_app()
You can control how often fixtures are called via pytest's fixture scope settings. |
Beta Was this translation helpful? Give feedback.
cs.store()
should work the same way in the forinitialize
+compose
as it does for@hydra.main
.The key is to make sure that you call
cs.store
before you callcompose
. I suspect that in your case, the call tocs.store
is not being run.If your call to
cs.store
is at the top level ofmy_configs.py
, you could import themy_configs
module in your pytest fixture. That should be enough to guarantee that theconfig_schema
node get stored.