question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Hydra resolver does not work in compose_api

See original GitHub issue

🐛 Bug

Description

hydra:key substitution does not work in interactive Python session and Jupyter notebook. hydra.key does, but it is not recommended (according to https://github.com/facebookresearch/hydra/issues/1786#issuecomment-914734857).

Checklist

  • I checked on the latest version of Hydra
  • I created a minimal repro (See this for tips).

To reproduce

** Minimal Code/Config snippet to reproduce **

conf/config.yaml

defaults:
  - env: dev

conf/env/prod.yaml

spark:
    appName: ${hydra:job.name}

conf/env/dev.yaml

spark:
    appName: ${hydra.job.name}

run.py

from hydra import initialize, compose

with initialize(config_path="conf"):
    conf = compose(config_name="config.yaml", return_hydra_config=True)
    print(conf.env.spark.appName)
    # prints current file name, e.g. "run"

with initialize(config_path="conf"):
    conf = compose(config_name="config.yaml", return_hydra_config=True, overrides=["env=prod"])
    print(conf.env.spark.appName)
    # raises exception

** Stack trace/error message **

$ python run.py
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/dictconfig.py", line 357, in __getattr__
    self._format_and_raise(key=key, value=None, cause=e)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/base.py", line 196, in _format_and_raise
    type_override=type_override,
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/_utils.py", line 821, in format_and_raise
    _raise(ex, cause)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/_utils.py", line 719, in _raise
    raise ex.with_traceback(sys.exc_info()[2])  # set end OC_CAUSE=1 for full backtrace
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/dictconfig.py", line 351, in __getattr__
    return self._get_impl(key=key, default_value=_DEFAULT_MARKER_)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/dictconfig.py", line 446, in _get_impl
    key=key, value=node, default_value=default_value
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/basecontainer.py", line 69, in _resolve_with_default
    throw_on_resolution_failure=True,
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/base.py", line 622, in _maybe_resolve_interpolation
    memo=memo if memo is not None else set(),
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/base.py", line 483, in _resolve_interpolation_from_parse_tree
    parse_tree=parse_tree, node=value, key=key, parent=parent, memo=memo
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/base.py", line 669, in resolve_parse_tree
    ).with_traceback(sys.exc_info()[2])
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/base.py", line 662, in resolve_parse_tree
    return visitor.visit(parse_tree)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/antlr4/tree/Tree.py", line 34, in visit
    return tree.accept(self)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/grammar/gen/OmegaConfGrammarParser.py", line 205, in accept
    return visitor.visitConfigValue(self)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/grammar_visitor.py", line 101, in visitConfigValue
    return self.visit(ctx.getChild(0))
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/antlr4/tree/Tree.py", line 34, in visit
    return tree.accept(self)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/grammar/gen/OmegaConfGrammarParser.py", line 339, in accept
    return visitor.visitText(self)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/grammar_visitor.py", line 298, in visitText
    return self.visitInterpolation(c)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/grammar_visitor.py", line 125, in visitInterpolation
    return self.visit(ctx.getChild(0))
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/antlr4/tree/Tree.py", line 34, in visit
    return tree.accept(self)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/grammar/gen/OmegaConfGrammarParser.py", line 1030, in accept
    return visitor.visitInterpolationResolver(self)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/grammar_visitor.py", line 182, in visitInterpolationResolver
    args_str=tuple(args_str),
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/base.py", line 653, in resolver_interpolation_callback
    inter_args_str=args_str,
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/base.py", line 596, in _evaluate_custom_resolver
    inter_args_str,
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/omegaconf/omegaconf.py", line 435, in resolver_wrapper
    ret = resolver(*args, **kwargs)
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/hydra/core/utils.py", line 215, in <lambda>
    lambda path: OmegaConf.select(cast(DictConfig, HydraConfig.get()), path),
  File "/home/maxim/Repo/ds-demo-project/venv/lib/python3.7/site-packages/hydra/core/hydra_config.py", line 31, in get
    raise ValueError("HydraConfig was not set")
omegaconf.errors.InterpolationResolutionError: ValueError raised while resolving interpolation: HydraConfig was not set
    full_key: env.spark.appName
    object_type=dict

Expected Behavior

Substitutionshydra: are not causing an exception inside a context created by compose function with return_hydra_config=True. HydraConfig instance should be initialized.

System information

  • Hydra Version : both 1.1.0 from pypi and 2a9f181 commit from main branch
  • Python version : both 3.7.8 and 3.10.2
  • Virtual environment type and version : venv
  • Operating system : Linux x86_64 5.16.5-1

Additional context

Same behavior has been observed several times: #716 https://github.com/facebookresearch/hydra/issues/1786#issuecomment-913110496

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
Jasha10commented, Feb 15, 2022

Yes indeed, the ConfigStore is persistent global state.

For Hydra’s test suite, we have a hydra_restore_singletons fixture that is used to save and then later restore the global state so that the tests can be run independently.

1reaction
Jasha10commented, Sep 22, 2022

Thanks for looking into this @odelalleau! I’ll try to put together an implementation for compose_context.

I’m thinking that a usage pattern involving OmegaConf.resolve could even make the cfg object behave nicely after the context has ended:

with initialize(...):
    with compose_context(...) as cfg:
        OmegaConf.resolve(cfg)  # makes cfg usable outside of the context
...
application_logic(cfg.foo)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Compose API - Hydra
The Compose API is useful when @hydra.main() is not applicable. ... Tab completion, Multirun, Working directory management, Logging management and more) ...
Read more >
Newest 'fb-hydra' Questions - Stack Overflow
I'm trying to run a Python script but it gives an error immediately at the first line of code while trying importing hydra...
Read more >
CHANGELOG.md - ory/hydra - Sourcegraph
Resolves an issues with post-release steps and adds the introspect command to the Ory Hydra CLI. Bug Fixes. Add missing introspect command (c43aba3);...
Read more >
modulus.hydra.utils — Modulus 22.09 documentation
import functools import hydra import os import torch import logging import copy ... DictConfig: """Internal Modulus config initializer and compose function.
Read more >
Bug listing with status RESOLVED with resolution OBSOLETE ...
Bug :1523 - "[IDEA] Offload work by distributing trivial ebuild ... Bug:90687 - "app-emulation/bochs: ebuild does not support pci nor pcidev linux kernel ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found