-
-
Notifications
You must be signed in to change notification settings - Fork 637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] replacing instatiate(ObjConf) with already instatiated object #1722
Comments
This is wrong: @dataclass
class ObjBConf:
_target_: str = 'test.ObjB'
ObjA: ObjAConf = MISSING the desired type for ObjA is not ObjAConf, but ObjA (the class, the fact you are using the same name for the field make things more confusing). |
But how do you deal with the relation (and types) between For example, the following two ways of initialisation result in equivalent objects (edited from the previous example following your comment about confusing variable names): from dataclasses import dataclass
from hydra.utils import instantiate
class ObjA:
def __init__(self, a: int):
self.a = a
class ObjB:
def __init__(self, obj_a: ObjA, b: int):
self.obj_a = obj_a
self.b = b
@dataclass
class ObjAConf:
_target_: str = 'test.ObjA'
a: int = 5
@dataclass
class ObjBConf:
_target_: str = 'test.ObjB'
obj_a: ObjAConf = ObjAConf
b: int = 7
# two different ways to obtain the same objects
obj_b_inst = instantiate(ObjBConf)
obj_b_init = ObjB(ObjA(a=5), b=7)
print(f"From instantiate: obj_b.b={obj_b_inst.b}, obj_b.obj_a.a={obj_b_inst.obj_a.a}")
print(f"From initialise: obj_b.b={obj_b_init.b}, obj_b.obj_a.a={obj_b_init.obj_a.a}")
# From instantiate: obj_b.b=7, obj_b.obj_a.a=5
# From initialise: obj_b.b=7, obj_b.obj_a.a=5 From the So my original question still somewhat stands, making the type of |
As I said:
|
Since OmegaConf doesn't allow non-primitive types, we need to specify object's configurations instead.
However, we might end up in a situation where one of the sub-objects is already initialized, and we wish to simply use this object as-is.
Naively replacing the object results in either a
ValidationError
(omegaconf.errors.ValidationError: Invalid type assigned
) orImportError: Encountered error: Invalid type assigned
(as expected), see the example below (save file astest.py
).One option is to manually create the object ourselves, extracting the configuration options as provided, but this forgoes the power of the more general
instantiate
function.Better would be a flag to indicate that the object attached to the config is already constructed, and to just ignore (the type of) this arg, but for now I don't see how this is possible at the moment.
The text was updated successfully, but these errors were encountered: