Skip to content

[Feature Request] Support _target_ to be a callable object itself in recursive construction #1017

@ppwwyyxx

Description

@ppwwyyxx

🚀 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:

  • the name is often too long, such as library.module_name.submodule_name.class
  • user writing a new class/function in code, and want to swap this class/function with the existing one in config.

Pitch

In https://github.com/facebookresearch/hydra/blob/6ac5a6270530ff4acf76248a01e11c3cc4278847/examples/instantiate/object_recursive/my_app.py, be able to use the following instead:

class MyNewDriver()
...

@hydra.main(config_name="config")
def my_app(cfg: DictConfig) -> None:
    cfg = OmegaConf.to_container(cfg)
    cfg['car']['driver']['_target_'] = MyNewDriver
    car: Car = instantiate(cfg['car'])
    car.drive()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions