From 6a4fd518d53c7b9996b720458352b1e6a643e769 Mon Sep 17 00:00:00 2001 From: Jasha10 <8935917+Jasha10@users.noreply.github.com> Date: Mon, 23 May 2022 20:03:45 -0500 Subject: [PATCH] allow conversion Path -> str (#941) --- news/934.bugfix | 1 + omegaconf/nodes.py | 2 +- tests/structured_conf/test_structured_config.py | 17 +++++++++++++++-- tests/test_nodes.py | 1 + tests/test_utils.py | 2 +- 5 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 news/934.bugfix diff --git a/news/934.bugfix b/news/934.bugfix new file mode 100644 index 000000000..2e40e45ba --- /dev/null +++ b/news/934.bugfix @@ -0,0 +1 @@ +Revert an accidental behavior change where implicit conversion from `Path` to `str` was disallowed. diff --git a/omegaconf/nodes.py b/omegaconf/nodes.py index adde9a1d2..1f02cde65 100644 --- a/omegaconf/nodes.py +++ b/omegaconf/nodes.py @@ -189,7 +189,7 @@ def _validate_and_convert_impl(self, value: Any) -> str: if ( OmegaConf.is_config(value) or is_primitive_container(value) - or isinstance(value, (bytes, Path)) + or isinstance(value, bytes) ): raise ValidationError("Cannot convert '$VALUE_TYPE' to string: '$VALUE'") return str(value) diff --git a/tests/structured_conf/test_structured_config.py b/tests/structured_conf/test_structured_config.py index c0055cc7f..e317d9436 100644 --- a/tests/structured_conf/test_structured_config.py +++ b/tests/structured_conf/test_structured_config.py @@ -71,8 +71,15 @@ class IntegersConfigAssignments: class StringConfigAssignments: - legal = ["10", "-10", "foo", "", (Color.BLUE, "Color.BLUE")] - illegal = [b"binary", Path("hello.txt")] + legal = [ + "10", + "-10", + "foo", + "", + (Color.BLUE, "Color.BLUE"), + (Path("hello.txt"), "hello.txt"), + ] + illegal = [b"binary"] class BytesConfigAssignments: @@ -2259,3 +2266,9 @@ def test_support_pep_604(self, module: Any) -> None: assert _utils.get_type_hint(cfg, "uis_with_default") == Union[int, str] assert cfg.uisn is None assert cfg.uis_with_default == 123 + + def test_assign_path_to_string_typed_field(self, module: Any) -> None: + cfg = OmegaConf.create(module.StringConfig) + cfg.null_default = Path("hello.txt") + assert isinstance(cfg.null_default, str) + assert cfg.null_default == "hello.txt" diff --git a/tests/test_nodes.py b/tests/test_nodes.py index 541d6f3f0..af7367042 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -596,6 +596,7 @@ def test_accepts_mandatory_missing( { "PathNode": copy.copy, "AnyNode": copy.copy, + "StringNode": str, }, id="path-data", ), diff --git a/tests/test_utils.py b/tests/test_utils.py index 66b4d3399..f859c16e3 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -146,7 +146,7 @@ def test_node_wrap( # str param(str, "foo", StringNode("foo"), id="str"), param(str, b"binary", ValidationError, id="str"), - param(str, Path("hello.txt"), ValidationError, id="str"), + param(str, Path("hello.txt"), StringNode("hello.txt"), id="str"), param(str, True, StringNode("True"), id="str"), param(str, 1, StringNode("1"), id="str"), param(str, 1.0, StringNode("1.0"), id="str"),