diff --git a/confit/config.py b/confit/config.py index 32f7661..eb93022 100644 --- a/confit/config.py +++ b/confit/config.py @@ -141,11 +141,12 @@ def from_cfg_str(cls, s: str, resolve: bool = False, registry: Any = None) -> An @classmethod def from_yaml_str(cls, s: str, resolve: bool = False, registry: Any = None) -> Any: import yaml + class ConfitYamlLoader(yaml.SafeLoader): def construct_object(self, x, deep=False): if isinstance(x, yaml.ScalarNode): if x.style == '"' or x.style == "'": - return loads(x.style+x.value+x.style) + return loads(x.style + x.value + x.style) return loads(x.value) return super().construct_object(x, deep) diff --git a/confit/utils/xjson.py b/confit/utils/xjson.py index 61e4739..1941527 100644 --- a/confit/utils/xjson.py +++ b/confit/utils/xjson.py @@ -272,7 +272,7 @@ def loads(s: str): if set(s) & set(",'\"{}[]$"): raise MalformedValueError(s) return s - + def dumps(o: Any): """ diff --git a/tests/test_config_instance.py b/tests/test_config_instance.py index b8b8f0e..ffd4038 100644 --- a/tests/test_config_instance.py +++ b/tests/test_config_instance.py @@ -699,8 +699,9 @@ def test_very_long_yaml_config(): def test_escaped_string(): - config = Config.from_yaml_str(""" -test: + config = Config.from_yaml_str( + """ +test: a: "1" section: num: 1 @@ -708,9 +709,10 @@ def test_escaped_string(): real_ref: ${test.a} escaped_broken_ref: "${test.a" escaped_ref: "${test.a}" -""").resolve(registry=registry) +""" + ).resolve(registry=registry) assert config["section"]["num"] == 1 assert config["section"]["escaped_num"] == "1" assert config["section"]["real_ref"] == "1" assert config["section"]["escaped_broken_ref"] == "${test.a" - assert config["section"]["escaped_ref"] == "${test.a}" \ No newline at end of file + assert config["section"]["escaped_ref"] == "${test.a}"