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

Fix/issue 26 : YAML Parsing for Large Config Files #28

Merged
merged 1 commit into from
Dec 17, 2024
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
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}"
Loading