def test_class_instantiate_pass_omegaconf_node() -> Any: conf = OmegaConf.structured( ObjectConf( target="tests.AClass", params={"b": 200, "c": {"x": 10, "y": "${params.b}"}}, ) ) obj = utils.instantiate(conf, **{"a": 10, "d": AnotherClass(99)}) assert obj == AClass(10, 200, {"x": 10, "y": 200}, AnotherClass(99)) assert OmegaConf.is_config(obj.c)
def test_class_instantiate_omegaconf_node() -> Any: conf = OmegaConf.structured( { "_target_": "tests.AClass", "b": 200, "c": {"x": 10, "y": "${b}"}, } ) obj = utils.instantiate(conf, a=10, d=AnotherClass(99)) assert obj == AClass(a=10, b=200, c={"x": 10, "y": 200}, d=AnotherClass(99)) assert OmegaConf.is_config(obj.c)
def test_class_instantiate_omegaconf_node(instantiate_func: Any, config: Any) -> Any: obj = instantiate_func(config, a=10, d=AnotherClass(99)) assert obj == AClass(a=10, b=200, c={ "x": 10, "y": 200 }, d=AnotherClass(99)) assert OmegaConf.is_config(obj.c)
def test_class_instantiate_objectconf_pass_omegaconf_node() -> Any: with pytest.warns(expected_warning=UserWarning) as recwarn: conf = OmegaConf.structured( ObjectConf( target="tests.AClass", params={ "b": 200, "c": { "x": 10, "y": "${params.b}" } }, )) obj = utils.instantiate(conf, **{"a": 10, "d": AnotherClass(99)}) assert obj == AClass(10, 200, {"x": 10, "y": 200}, AnotherClass(99)) assert OmegaConf.is_config(obj.c) assert recwarn[0].message.args[0] == objectconf_depreacted assert recwarn[1].message.args[0] == target_field_deprecated.format( field="target")
def test_override_target(instantiate_func: Any, config: Any, passthrough: Any, expected: Any) -> None: obj = instantiate_func(config, **passthrough) assert obj == expected @mark.parametrize( "config, passthrough, expected", [ param( { "_target_": tests.AnotherClass, "x": 10 }, {}, AnotherClass(10), id="class_in_config_dict", ), ], ) def test_instantiate_from_class_in_dict(instantiate_func: Any, config: Any, passthrough: Any, expected: Any) -> None: config_copy = copy.deepcopy(config) assert instantiate_func(config, **passthrough) == expected assert config == config_copy @mark.parametrize( "config, passthrough, expected", [