Skip to content

Commit

Permalink
BUG: fix a bug where special strings 'true', 't', 'false' and 'f' wou…
Browse files Browse the repository at this point in the history
…ld decay to boolean after two parsing cycles
  • Loading branch information
neutrinoceros committed Apr 28, 2022
1 parent e3a3244 commit 1243409
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 3 additions & 1 deletion inifix/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ def _from_path(file: PathLike) -> InifixConfT:
def _encode(v: Scalar) -> str:
if isinstance(v, float):
return ENotationIO.encode_preferential(v)
elif isinstance(v, str) and re.search(r"\s", v) is not None:
elif isinstance(v, str) and (
re.search(r"\s", v) is not None or v in ("true", "t", "false", "f")
):
return repr(v)
else:
return str(v)
Expand Down
17 changes: 13 additions & 4 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def test_idempotent_io(inifile):
("""val 1e2\n""", {"val": 100}),
("""val "1e2"\n""", {"val": "1e2"}),
("""val '1e2'\n""", {"val": "1e2"}),
('''[Spam]\nEggs "Bacon Saussage"''', {"Spam": {"Eggs": "Bacon Saussage"}}),
(
"name true 'true' 'steven bacon' 1",
{"name": [True, "true", "steven bacon", 1]},
Expand All @@ -78,9 +77,19 @@ def test_string_casting(data, expected):
assert mapping == expected


def test_idempotent_string_parsing():
initial_str = '''[Spam]\nEggs "Bacon Saussage"'''
initial_mapping = loads(initial_str)
@pytest.mark.parametrize(
"data, expected",
[
('''[Spam]\nEggs "Bacon Saussage"''', {"Spam": {"Eggs": "Bacon Saussage"}}),
(
"name true 'true' 'steven bacon' 1",
{"name": [True, "true", "steven bacon", 1]},
),
],
)
def test_idempotent_string_parsing(data, expected):
initial_mapping = loads(data)
assert initial_mapping == expected
round_str = dumps(initial_mapping)
round_mapping = loads(round_str)
assert round_mapping == initial_mapping
Expand Down

0 comments on commit 1243409

Please sign in to comment.