Skip to content

Commit

Permalink
Fix: add support for YAML config files over 4096 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasDedieu committed Dec 17, 2024
1 parent ef01561 commit f5bae0d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion confit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ def from_yaml_str(cls, s: str, resolve: bool = False, registry: Any = None) -> A
class ConfitYamlLoader(yaml.SafeLoader):
def construct_object(self, x, deep=False):
if isinstance(x, yaml.ScalarNode):
return loads(self.buffer[x.start_mark.index : x.end_mark.index])
if x.style == '"' or x.style == "'":
return loads(x.style + x.value + x.style)
return loads(x.value)
return super().construct_object(x, deep)

stream = StringIO(s)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_config_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,34 @@ def test_if_else_complex():
"""
).resolve(registry=registry)
assert config["params"]["c"] == 12


def test_very_long_yaml_config():
config = Config.from_yaml_str(
"""\
a: {}
""".format(
"x" * 4200
)
).resolve(registry=registry)
assert config == {"a": "x" * 4200}


def test_escaped_string():
config = Config.from_yaml_str(
"""
test:
a: "1"
section:
num: 1
escaped_num: "1"
real_ref: ${test.a}
escaped_broken_ref: "${test.a"
escaped_ref: "${test.a}"
"""
).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}"

0 comments on commit f5bae0d

Please sign in to comment.