Skip to content

How to define a class type in Hydra, without instantiation? #2101

Answered by Jasha10
FeryET asked this question in Q&A
Discussion options

You must be logged in to vote

If you know ahead of time what the type will be, you could register an OmegaConf custom resolver like this:

from omegaconf import OmegaConf

class Foo:
    ...

def get_Foo():
    return Foo

OmegaConf.register_new_resolver("getFoo", get_Foo)

yaml_data = """
cls: ${getFoo:}
"""

optim_params = OmegaConf.create(yaml_data)
print(optim_params.cls)  # prints <class '__main__.Foo'>
print(type(optim_params.cls))  # prints <class 'type'>

If you do not know the class ahead of time, you can try registering the hydra.utils.get_class method as a custom resolver:

from omegaconf import OmegaConf
from hydra.utils import get_class

class Foo1:
    ...

class Foo2:
    ...

OmegaConf.register_new_resolver(

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by FeryET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants