Skip to content

Commit

Permalink
Fix override str util function to handle new lines and quotes correct…
Browse files Browse the repository at this point in the history
  • Loading branch information
jesszzzz authored Oct 28, 2024
1 parent 103a99a commit e671ee5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
3 changes: 3 additions & 0 deletions hydra/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,7 @@ def to_hydra_override_value_str(obj: Any) -> str:
return (
"[" + ", ".join([to_hydra_override_value_str(value) for value in obj]) + "]"
)
elif isinstance(obj, str):
new_str = obj.replace('\\"', '\\\\"').replace('"', '\\"')
return f'"{new_str}"'
return json.dumps(obj)
30 changes: 20 additions & 10 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import io
import json
import os
import re
from pathlib import Path
Expand All @@ -15,6 +16,7 @@
from hydra._internal.utils import run_and_report
from hydra.conf import HydraConf, RuntimeConf
from hydra.core.hydra_config import HydraConfig
from hydra.core.override_parser.overrides_parser import OverridesParser
from hydra.errors import HydraDeprecationError
from hydra.test_utils.test_utils import (
assert_multiline_regex_search,
Expand Down Expand Up @@ -74,22 +76,30 @@ def test_to_absolute_path_without_hydra(


@mark.parametrize(
"obj, expected",
"obj",
[
("foo bar", '"foo bar"'),
(10, "10"),
({"foo": '\\"bar'}, '{foo: "\\\\\\"bar"}'),
([1, 2, "3", {"a": "xyz"}], '[1, 2, "3", {a: "xyz"}]'),
("foo bar"),
(10),
({"foo": '\\"bar\\\'"'}),
([1, 2, "3", {"a": "xyz"}]),
({"a": 10, "b": "c", "d": {"e": [1, 2, "3"], "f": ["g", {"h": {"i": "j"}}]}}),
(
{"a": 10, "b": "c", "d": {"e": [1, 2, "3"], "f": ["g", {"h": {"i": "j"}}]}},
'{a: 10, b: "c", d: {e: [1, 2, "3"], f: ["g", {h: {i: "j"}}]}}',
{
"a": 10,
"b": "c\nnl",
"d": {"e": [1, 2, "3"], "f": ["g", {"h": {"i": "j"}}]},
}
),
({"json_val": json.dumps({"a": 10, "b": "c\\\nnl"}, indent=4)}),
],
)
def test_to_hydra_override_value_str(
hydra_restore_singletons: Any, obj: Any, expected: str
def test_to_hydra_override_value_str_roundtrip(
hydra_restore_singletons: Any, obj: Any
) -> None:
assert utils.to_hydra_override_value_str(obj) == expected
override_str = utils.to_hydra_override_value_str(obj)
override_params = f"++ov={override_str}"
o = OverridesParser.create().parse_override(override_params)
assert o.value() == obj


@mark.parametrize(
Expand Down

0 comments on commit e671ee5

Please sign in to comment.