Skip to content

Commit b51cee5

Browse files
authored
fix: warn instead of fail when suspecting malformed configs (#44)
1 parent 13feb6c commit b51cee5

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.10.3 (2025-01-19)
4+
5+
- Don't fail but warn, when we suspect that un unquoted string might actually be an object in config files.
6+
37
## v0.10.2 (2025-12-11)
48

59
- Confit should not complain anymore when given multiline strings

confit/config.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from confit.errors import (
1515
ConfitValidationError,
1616
CyclicReferenceError,
17-
ErrorWrapper,
1817
MissingReference,
1918
patch_errors,
2019
remove_lib_from_traceback,
@@ -120,11 +119,7 @@ def from_cfg_str(cls, s: str, resolve: bool = False, registry: Any = None) -> An
120119
current.clear()
121120
errors = []
122121
for k, v in parser.items(section):
123-
parsed_k = loads(k)
124-
try:
125-
current[parsed_k] = loads(v)
126-
except ValueError as e:
127-
errors.append(ErrorWrapper(e, loc=parsed_k))
122+
current[loads(k)] = loads(v)
128123

129124
if errors:
130125
raise ConfitValidationError(errors=errors)
@@ -425,9 +420,9 @@ def rec(obj, loc: Tuple[Union[str, int]] = ()):
425420
for key, value in resolved.items()
426421
if isinstance(key, str) and key.startswith("@")
427422
]
428-
assert (
429-
len(registries) <= 1
430-
), f"Cannot resolve using multiple registries at {'.'.join(loc)}"
423+
assert len(registries) <= 1, (
424+
f"Cannot resolve using multiple registries at {'.'.join(loc)}"
425+
)
431426

432427
if len(registries) == 1:
433428
raw_key, value, registry_value = registries[0]
@@ -437,7 +432,7 @@ def rec(obj, loc: Tuple[Union[str, int]] = ()):
437432
value = value.strip()
438433
is_draft = value.endswith("!draft")
439434
if is_draft:
440-
value = value[:-len("!draft")].strip()
435+
value = value[: -len("!draft")].strip()
441436
fn = registry_value.get(value)
442437
try:
443438
if is_draft:

confit/utils/xjson.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ast
2+
import warnings
23
from typing import Any, Callable
34

45
from lark import Lark, Transformer, Tree
@@ -253,12 +254,6 @@ def _iterencode(o):
253254
return _iterencode
254255

255256

256-
class MalformedValueError(ValueError):
257-
def __init__(self, value: str):
258-
self.value = value
259-
super().__init__(f"Malformed value: {value!r}")
260-
261-
262257
def loads(s: str):
263258
"""
264259
Load an extended JSON string into a python object.
@@ -280,7 +275,10 @@ def loads(s: str):
280275
# Fail if we suspect that it is a malformed object
281276
# (e.g. has ', ", {, }, [, ] in it)
282277
if set(s) & set(",'\"{}[]$"):
283-
raise MalformedValueError(s)
278+
warnings.warn(
279+
f"Some values may be malformed JSON objects. Got: {s!r}",
280+
UserWarning,
281+
)
284282
return s
285283

286284

tests/test_config_instance.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,22 +556,23 @@ def test_list_interpolation():
556556
assert config["section"]["b"] == ["foo", "bar", "baz"]
557557

558558

559-
def test_fail_if_suspected_json_malformation():
560-
with pytest.raises(ConfitValidationError) as exc_info:
561-
Config.from_str(
559+
def test_warn_if_suspected_json_malformation():
560+
with pytest.warns(UserWarning) as record:
561+
config = Config.from_str(
562562
"""
563563
[section]
564564
string = 'ok
565565
list = 'ok']
566566
"""
567567
)
568-
assert str(exc_info.value) == (
569-
"2 validation errors\n"
570-
"-> string\n"
571-
' Malformed value: "\'ok"\n'
572-
"-> list\n"
573-
" Malformed value: \"'ok']\""
574-
)
568+
569+
assert len(record) == 2
570+
assert [str(w.message) for w in record] == [
571+
'Some values may be malformed JSON objects. Got: "\'ok"',
572+
"Some values may be malformed JSON objects. Got: \"'ok']\"",
573+
]
574+
575+
assert config == {"section": {"string": "'ok", "list": "'ok']"}}
575576

576577

577578
def test_string():

0 commit comments

Comments
 (0)