Skip to content

Commit

Permalink
Merge pull request #22 from explosion/ux/warn-single-quotes
Browse files Browse the repository at this point in the history
Add check for single quoted values
  • Loading branch information
polm authored Jan 12, 2023
2 parents f950fe0 + 5e5a46b commit f7b34c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 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,6 +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 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."
)
except Exception:
pass
return super().before_read(parser, section, option, value)
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)

0 comments on commit f7b34c6

Please sign in to comment.