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

Add check for single quoted values #22

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 7 additions & 2 deletions confection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io
import copy
import re
import warnings

from .util import Decorator

Expand Down Expand Up @@ -43,8 +44,12 @@ def before_read(self, parser, section, option, value):
json_value = srsly.json_loads(value)
if isinstance(json_value, str) and json_value not in JSON_EXCEPTIONS:
value = json_value
except Exception:
pass
except ValueError:
if value and value[0] == value[-1] == "'":
warnings.warn(
f"The value [{value}] seems to be single-quoted, but values "
"use JSON formatting, which requires double quotes."
)
polm marked this conversation as resolved.
Show resolved Hide resolved
return super().before_read(parser, section, option, value)

def before_get(self, parser, section, option, value, defaults):
Expand Down
17 changes: 17 additions & 0 deletions confection/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,3 +1379,20 @@ def test_config_overrides(greeting, value, expected):
assert "${vars.a}" in str_cfg
cfg = Config().from_str(str_cfg, overrides=overrides)
assert expected in str(cfg)


def test_warn_single_quotes():
str_cfg = f"""
[project]
commands = 'do stuff'
"""

with pytest.warns(UserWarning, match="single-quoted"):
cfg = Config().from_str(str_cfg)

# should not warn if single quotes are in the middle
str_cfg = f"""
[project]
commands = some'thing
"""
cfg = Config().from_str(str_cfg)