def test_object_conf_deprecated() -> None: with pytest.warns(UserWarning) as recwarn: cfg = ObjectConf(target="tests.AClass", params={"a": 10, "b": 20}) ret = utils.instantiate(cfg, c=30) assert ret == AClass(a=10, b=20, c=30) assert recwarn[0].message.args[0] == objectconf_depreacted assert recwarn[1].message.args[0] == target_field_deprecated.format( field="target")
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_instantiate_deprecations() -> None: expected = AClass(10, 20, 30, 40) with pytest.warns(UserWarning): config = OmegaConf.structured( {"class": "tests.AClass", "params": {"a": 10, "b": 20, "c": 30, "d": 40}} ) assert utils.instantiate(config) == expected with pytest.warns(UserWarning): config = OmegaConf.structured( {"cls": "tests.AClass", "params": {"a": 10, "b": 20, "c": 30, "d": 40}} ) assert utils.instantiate(config) == expected config = OmegaConf.structured( {"target": "tests.AClass", "params": {"a": 10, "b": 20, "c": 30, "d": 40}} ) assert utils.instantiate(config) == expected
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_a_class_eq() -> None: assert AClass(a=10, b=20, c=30, d=40) != AClass(a=11, b=12, c=13, d=14) assert AClass(a=10, b=20, c=30, d=40) == AClass(a=10, b=20, c=30, d=40)
def test_pass_extra_variables() -> None: cfg = OmegaConf.create({"_target_": "tests.AClass", "a": 10, "b": 20}) assert utils.instantiate(cfg, c=30) == AClass(a=10, b=20, c=30)
def test_instantiate_deprecations() -> None: expected = AClass(10, 20, 30, 40) with pytest.warns(UserWarning, match=target_field_deprecated.format(field="class")): config = OmegaConf.structured({ "class": "tests.AClass", "params": { "a": 10, "b": 20, "c": 30, "d": 40 } }) assert utils.instantiate(config) == expected with pytest.warns(UserWarning, match=target_field_deprecated.format(field="cls")): config = OmegaConf.structured({ "cls": "tests.AClass", "params": { "a": 10, "b": 20, "c": 30, "d": 40 } }) assert utils.instantiate(config) == expected with pytest.warns(UserWarning, match=target_field_deprecated.format(field="target")): config = OmegaConf.structured({ "target": "tests.AClass", "params": { "a": 10, "b": 20, "c": 30, "d": 40 } }) assert utils.instantiate(config) == expected with pytest.warns(UserWarning, match=params_field_deprecated): config = OmegaConf.structured({ "_target_": "tests.AClass", "params": { "a": 10, "b": 20, "c": 30, "d": 40 } }) assert utils.instantiate(config) == expected # normal case, no warnings config = OmegaConf.structured({ "_target_": "tests.AClass", "a": 10, "b": 20, "c": 30, "d": 40 }) assert utils.instantiate(config) == expected
assert AClass(a=10, b=20, c=30, d=40) == AClass(a=10, b=20, c=30, d=40) @pytest.mark.parametrize( # type: ignore "input_conf, passthrough, expected", [ pytest.param( { "_target_": "tests.AClass", "a": 10, "b": 20, "c": 30, "d": 40 }, {}, AClass(10, 20, 30, 40), id="class", ), pytest.param( { "_target_": "tests.AClass", "b": 20, "c": 30 }, { "a": 10, "d": 40 }, AClass(10, 20, 30, 40), id="class+override", ),
def test_pass_extra_variables() -> None: cfg = ObjectConf(target="tests.AClass", params={"a": 10, "b": 20}) assert utils.instantiate(cfg, c=30) == AClass(a=10, b=20, c=30)
@pytest.mark.parametrize( # type: ignore "recursive", [ pytest.param(False, id="non_recursive"), pytest.param(True, id="recursive"), ], ) @pytest.mark.parametrize( # type: ignore "input_conf, passthrough, expected", [ pytest.param( {"_target_": "tests.AClass", "a": 10, "b": 20, "c": 30, "d": 40}, {}, AClass(10, 20, 30, 40), id="class", ), pytest.param( {"_target_": "tests.AClass", "b": 20, "c": 30}, {"a": 10, "d": 40}, AClass(10, 20, 30, 40), id="class+override", ), pytest.param( {"_target_": "tests.AClass", "b": 200, "c": "${b}"}, {"a": 10, "b": 99, "d": 40}, AClass(10, 99, 99, 40), id="class+override+interpolation", ), # Check class and static methods