Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow conversion Path -> str #941

Merged
merged 2 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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