Repository: google/yapf
Pull request #122 (2015-04-22 05:38:44)

Be less strict with dict-style input.
Accept {INDENT_WIDTH=4} or {INDENT_WIDTH : 2}
Raise StyleConfigError if given bad input.

cc #105 @sawan.


Patch:
diff --git a/yapf/__init__.py b/yapf/__init__.py
index 0d64ad4..f3c12de 100644
--- a/yapf/__init__.py
+++ b/yapf/__init__.py
@@ -72,7 +72,7 @@ def main(argv):
             'the default.'))
   parser.add_argument('--verify',
                       action='store_true',
-                      help='try to verify reformatted code for syntax errors')
+                      help='try to verify refomatted code for syntax errors')
   diff_inplace_group = parser.add_mutually_exclusive_group()
   diff_inplace_group.add_argument('-d', '--diff',
                                   action='store_true',
diff --git a/yapf/yapflib/style.py b/yapf/yapflib/style.py
index baabe4e..83a24ac 100644
--- a/yapf/yapflib/style.py
+++ b/yapf/yapflib/style.py
@@ -215,9 +215,12 @@ def CreateStyleFromConfig(style_config):
 
 def _CreateConfigParserFromConfigString(config_string):
   """Given a config string from the command line, return a config parser."""
+  if config_string[0] != "{" or config_string[-1] != "}":
+    raise StyleConfigError(
+        "Invalid style dict syntax: '{}'.".format(config_string))
   config = py3compat.ConfigParser()
   config.add_section('style')
-  for key, value in re.findall(r'([a-zA-Z0-9_]*): *([a-zA-Z0-9_]+)',
+  for key, value in re.findall(r'([a-zA-Z0-9_]+)\s*[:=]\s*([a-zA-Z0-9_]+)',
                                config_string):
     config.set('style', key, value)
   return config
@@ -227,14 +230,14 @@ def _CreateConfigParserFromConfigFile(config_filename):
   """Read the file and return a ConfigParser object."""
   if not os.path.exists(config_filename):
     # Provide a more meaningful error here.
-    raise StyleConfigError('"{0}" is not a valid style or file path'.format(
-        config_filename))
+    raise StyleConfigError(
+        '"{0}" is not a valid style or file path'.format(config_filename))
   with open(config_filename) as style_file:
     config = py3compat.ConfigParser()
     config.read_file(style_file)
     if not config.has_section('style'):
-      raise StyleConfigError('Unable to find section [style] in {0}'.format(
-          config_filename))
+      raise StyleConfigError(
+          'Unable to find section [style] in {0}'.format(config_filename))
     return config
 
 
@@ -265,7 +268,11 @@ def _CreateStyleFromConfigParser(config):
     option = option.upper()
     if option not in _STYLE_OPTION_VALUE_CONVERTER:
       raise StyleConfigError('Unknown style option "{0}"'.format(option))
-    base_style[option] = _STYLE_OPTION_VALUE_CONVERTER[option](value)
+    try:
+      base_style[option] = _STYLE_OPTION_VALUE_CONVERTER[option](value)
+    except ValueError:
+      raise StyleConfigError(
+          "'{}' is not a valid setting for {}.".format(value, option))
   return base_style
 
 


Test patch:
diff --git a/yapftests/style_test.py b/yapftests/style_test.py
index f11485f..62b5507 100644
--- a/yapftests/style_test.py
+++ b/yapftests/style_test.py
@@ -196,6 +196,24 @@ class StyleFromCommandLine(unittest.TestCase):
     self.assertTrue(_LooksLikeYapfStyle(cfg))
     self.assertEqual(cfg['INDENT_WIDTH'], 2)
 
+  def testDefaultBasedOnStyleNotStrict(self):
+    cfg = style.CreateStyleFromConfig(
+        '{based_on_style : pep8'
+        ' ,indent_width=2'
+        ' blank_line_before_nested_class_or_def:True}')
+    self.assertTrue(_LooksLikeYapfStyle(cfg))
+    self.assertEqual(cfg['INDENT_WIDTH'], 2)
+
+  def testDefaultBasedOnStyleBadString(self):
+    self.assertRaisesRegexp(style.StyleConfigError, "Unknown style option",
+                            style.CreateStyleFromConfig,
+                            '{based_on_styl: pep8}')
+    self.assertRaisesRegexp(style.StyleConfigError, "not a valid",
+                            style.CreateStyleFromConfig, '{INDENT_WIDTH: FOUR}')
+    self.assertRaisesRegexp(style.StyleConfigError, "Invalid style dict",
+                            style.CreateStyleFromConfig,
+                            '{based_on_style: pep8')
+
 
 class StyleHelp(unittest.TestCase):