-
-
Notifications
You must be signed in to change notification settings - Fork 656
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
[Feature Request] Support _target_ to be a callable object itself in recursive construction #1017
Comments
One thing that would work well with this is the ability to override the car: Car = instantiate(cfg.car, driver={"_target_": MyNewDriver})
# or the equivalent:
car: Car = instantiate(cfg.car, **{"driver": {"_target_": MyNewDriver}}) I will look into supporting both features, although the callable name can be obtained via name: car: Car = instantiate(cfg.car, driver={"_target_": MyNewDriver.__name__}) |
One this that I am not sure about here is in what scenario this is preferred over config composition. Do you have an example where building the config at runtime is somehow preferred to allowing Hydra to compose it? |
Upon an attempt to implement this I realized this is in conflict with an existing feature: @dataclass
class Foo:
x : float = 10 and you assign it to an OmegaConf container cfg = OmegaConf.create()
cfg.foo = Foo The result is equivalent to assigning an object of that type: cfg = OmegaConf.create()
cfg.foo = Foo() This is only true for dataclasses. however as they become more pervasive I am concerned that supporting this feature will cause confusion to people. Obtaining a string you can use from a class or callable is straight forward and you don't need to type the whole thing: def get_full_name(type_or_callable : Any) -> str:
return f"{type_or_callable.__module__}.{type_or_callable.__name__}" I think supporting this feature will - in the long term - cause more problems than it's solving. |
I think I was able to work around it in #1019 by converting input types to string (as in
This kind of support extends to both the config object (which can be a regular dict or a DictConfig) and also to the parameter overrides. That's why it was pretty hard to support type there (as OmegaConf has it's own interpretation of dataclass types). |
Thanks, I think that will work for all common scenarios. Note that however the extra round trip from callable -> string -> callable may not reliably work for everything, for example it's not obvious whether it works for lambda, |
Thanks for pointing it out. If there are specific cases where this can work but does not we can look at them and try to improve. |
🚀 Feature Request
Now
_target_
has to be the full name of the callable object, supporting_target_
to be a callable object itself in recursive construction would be very helpful.Motivation
In some situation there are some advantages using the callable directly:
library.module_name.submodule_name.class
Pitch
In https://github.com/facebookresearch/hydra/blob/6ac5a6270530ff4acf76248a01e11c3cc4278847/examples/instantiate/object_recursive/my_app.py, be able to use the following instead:
The text was updated successfully, but these errors were encountered: