Skip to content

Commit

Permalink
allow conversion Path -> str (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 committed May 24, 2022
1 parent 22a1e34 commit 6a4fd51
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions news/934.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Revert an accidental behavior change where implicit conversion from `Path` to `str` was disallowed.
2 changes: 1 addition & 1 deletion omegaconf/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions tests/structured_conf/test_structured_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
1 change: 1 addition & 0 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ def test_accepts_mandatory_missing(
{
"PathNode": copy.copy,
"AnyNode": copy.copy,
"StringNode": str,
},
id="path-data",
),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 6a4fd51

Please sign in to comment.