From 5f370d066f017bd0317659e45f36096b4cf1781c Mon Sep 17 00:00:00 2001 From: Paul O'Leary McCann Date: Thu, 12 Jan 2023 15:13:18 +0900 Subject: [PATCH 1/5] Add check for single quoted values --- confection/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/confection/__init__.py b/confection/__init__.py index 3f1db3d..feef7bc 100644 --- a/confection/__init__.py +++ b/confection/__init__.py @@ -44,7 +44,11 @@ def before_read(self, parser, section, option, value): if isinstance(json_value, str) and json_value not in JSON_EXCEPTIONS: value = json_value except Exception: - pass + if value and value[0] == "'" and value[-1] == "'": + warnings.warn( + f"The value [{value}] seems to be single-quoted, but values " + "use JSON formatting, which requires double quotes." + ) return super().before_read(parser, section, option, value) def before_get(self, parser, section, option, value, defaults): From b0ea5630cc0579ef6decbb0070240ae94fc83680 Mon Sep 17 00:00:00 2001 From: Paul O'Leary McCann Date: Thu, 12 Jan 2023 15:19:34 +0900 Subject: [PATCH 2/5] Add test --- confection/__init__.py | 1 + confection/tests/test_config.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/confection/__init__.py b/confection/__init__.py index feef7bc..4b77a30 100644 --- a/confection/__init__.py +++ b/confection/__init__.py @@ -16,6 +16,7 @@ import io import copy import re +import warnings from .util import Decorator diff --git a/confection/tests/test_config.py b/confection/tests/test_config.py index d0a90ab..6b2c730 100644 --- a/confection/tests/test_config.py +++ b/confection/tests/test_config.py @@ -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) From 6436eabac9d110f2a57d631bb9cdacff4013702e Mon Sep 17 00:00:00 2001 From: Paul O'Leary McCann Date: Thu, 12 Jan 2023 17:47:40 +0900 Subject: [PATCH 3/5] Update confection/__init__.py Co-authored-by: Raphael Mitsch --- confection/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/confection/__init__.py b/confection/__init__.py index 4b77a30..eab15c5 100644 --- a/confection/__init__.py +++ b/confection/__init__.py @@ -45,7 +45,7 @@ def before_read(self, parser, section, option, value): if isinstance(json_value, str) and json_value not in JSON_EXCEPTIONS: value = json_value except Exception: - if value and value[0] == "'" and value[-1] == "'": + 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." From 4097cad9d96c93424520b2b7eeec1bb842dbb39a Mon Sep 17 00:00:00 2001 From: Paul O'Leary McCann Date: Thu, 12 Jan 2023 17:58:46 +0900 Subject: [PATCH 4/5] Use a more specific error type --- confection/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/confection/__init__.py b/confection/__init__.py index eab15c5..46ee633 100644 --- a/confection/__init__.py +++ b/confection/__init__.py @@ -44,7 +44,7 @@ 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: + except ValueError: if value and value[0] == value[-1] == "'": warnings.warn( f"The value [{value}] seems to be single-quoted, but values " From 5e5a46bf780c1b913f3fa5e3087ebc270cc349e4 Mon Sep 17 00:00:00 2001 From: Paul O'Leary McCann Date: Thu, 12 Jan 2023 18:01:41 +0900 Subject: [PATCH 5/5] Update confection/__init__.py Co-authored-by: Raphael Mitsch --- confection/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/confection/__init__.py b/confection/__init__.py index 46ee633..9c6b677 100644 --- a/confection/__init__.py +++ b/confection/__init__.py @@ -50,6 +50,8 @@ def before_read(self, parser, section, option, value): 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) def before_get(self, parser, section, option, value, defaults):