hydra_zen.get_target#

hydra_zen.get_target(obj)[source]#

Returns the target-object from a targeted config.

Parameters:
objHasTarget

An object with a _target_ attribute.

Returns:
targetAny

The target object of the config.

Note that this will import the object using the import path specified by the config.

Raises:
TypeError: obj does not have a _target_ attribute.

Examples

Basic usage

get_target works on all variety of configs produced by builds and just.

>>> from hydra_zen import builds, just, get_target
>>> get_target(builds(int))
<class 'int'>
>>> get_target(builds(int, zen_partial=True))
<class 'int'>
>>> get_target(just(str))
<class 'str'>

It works for manually-defined configs:

>>> from dataclasses import dataclass
>>> @dataclass
... class A:
...     _target_: str = "builtins.dict"
>>> get_target(A())
<class 'dict'>

and for configs loaded from yamls.

>>> from hydra_zen import load_from_yaml, save_as_yaml
>>> class B: pass
>>> Conf = builds(B)
>>> save_as_yaml(Conf, "config.yaml")
>>> loaded_conf = load_from_yaml("config.yaml")

Note that the target of loaded_conf can be accessed without instantiating the config.

>>> get_target(loaded_conf)  # type: ignore
__main__.B