Repository: google/yapf
Pull request #296 (2016-08-29 07:17:01)

Support optional spaces for default and named assigns.

googlebot: We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s).  If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA ([login here](https://cla.developers.google.com/) to double check)?  If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.

<!-- need_author_cla -->
coveralls: 
[![Coverage Status](https://coveralls.io/builds/7646991/badge)](https://coveralls.io/builds/7646991)

Coverage remained the same at 97.034% when pulling **55b414fefb8b9b70a8f891f392a256c84f307df6 on alenz33:master** into **032d63209ece4d3a92f98861530802e0b1f760cb on google:master**.

googlebot: CLAs look good, thanks!

<!-- ok -->
coveralls: 
[![Coverage Status](https://coveralls.io/builds/7647007/badge)](https://coveralls.io/builds/7647007)

Coverage remained the same at 97.034% when pulling **f7946daa352fdf56c6b459ac50948da0b7118ea1 on alenz33:master** into **032d63209ece4d3a92f98861530802e0b1f760cb on google:master**.

gwelymernans: Thanks, @alenz33. Could you add some tests? (`yapftest/reformatter_test.py` is probably the place you want.) Also update `CHANGELOG` and `README.rst`.
alenz33: @gwelymernans 
Is that the correct way to update the CHANGELOG file?

Patch:
diff --git a/CHANGELOG b/CHANGELOG
index cc37458..04461f9 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -7,6 +7,8 @@
 - Support formatting of typed names. Typed names are formatted a similar way to
   how named arguments are formatted, except that there's a space after the
   colon.
+- Add a knob, 'SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN', to allow adding spaces
+  around the assign operator on default or named assigns.
 
 ## [0.11.1] 2016-08-17
 ### Changed
diff --git a/README.rst b/README.rst
index d86ce21..ab6f3cf 100644
--- a/README.rst
+++ b/README.rst
@@ -374,6 +374,10 @@ Knobs
 ``SPACES_AROUND_POWER_OPERATOR``
     Set to ``True`` to prefer using spaces around ``**``.
 
+``SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN``
+    Set to ``True`` to prefer spaces around the assignment operator for default
+    or keyword arguments.
+
 ``SPACES_BEFORE_COMMENT``
     The number of spaces required before a trailing comment.
 
diff --git a/yapf/yapflib/style.py b/yapf/yapflib/style.py
index 5642021..cebbc27 100644
--- a/yapf/yapflib/style.py
+++ b/yapf/yapflib/style.py
@@ -117,6 +117,8 @@ _STYLE_HELP = dict(
       Join short lines into one line. E.g., single line 'if' statements."""),
     SPACES_AROUND_POWER_OPERATOR=textwrap.dedent("""\
       Use spaces around the power operator."""),
+    SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN=textwrap.dedent("""\
+      Use spaces around default or named assigns."""),
     SPACES_BEFORE_COMMENT=textwrap.dedent("""\
       The number of spaces required before a trailing comment."""),
     SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=textwrap.dedent("""\
@@ -187,6 +189,7 @@ def CreatePEP8Style():
       JOIN_MULTIPLE_LINES=True,
       SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=True,
       SPACES_AROUND_POWER_OPERATOR=False,
+      SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN=False,
       SPACES_BEFORE_COMMENT=2,
       SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED=False,
       SPLIT_BEFORE_BITWISE_OPERATOR=False,
@@ -279,6 +282,7 @@ _STYLE_OPTION_VALUE_CONVERTER = dict(
     JOIN_MULTIPLE_LINES=_BoolConverter,
     SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=_BoolConverter,
     SPACES_AROUND_POWER_OPERATOR=_BoolConverter,
+    SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN=_BoolConverter,
     SPACES_BEFORE_COMMENT=int,
     SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED=_BoolConverter,
     SPLIT_BEFORE_BITWISE_OPERATOR=_BoolConverter,
diff --git a/yapf/yapflib/unwrapped_line.py b/yapf/yapflib/unwrapped_line.py
index 831b249..a93789e 100644
--- a/yapf/yapflib/unwrapped_line.py
+++ b/yapf/yapflib/unwrapped_line.py
@@ -256,7 +256,7 @@ def _SpaceRequiredBetween(left, right):
       format_token.Subtype.DEFAULT_OR_NAMED_ASSIGN in right.subtypes):
     # A named argument or default parameter shouldn't have spaces around it.
     # However, a typed argument should have a space after the colon.
-    return lval == ':'
+    return lval == ':' or style.Get('SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN')
   if (format_token.Subtype.VARARGS_STAR in left.subtypes or
       format_token.Subtype.KWARGS_STAR_STAR in left.subtypes):
     # Don't add a space after a vararg's star or a keyword's star-star.


Test patch:
diff --git a/yapftests/reformatter_test.py b/yapftests/reformatter_test.py
index fdc944b..d503183 100644
--- a/yapftests/reformatter_test.py
+++ b/yapftests/reformatter_test.py
@@ -3303,6 +3303,24 @@ class TestsForPython3Code(ReformatterTest):
     finally:
       style.SetGlobalStyle(style.CreatePEP8Style())
 
+  def testSpacesAroundDefaultOrNamedAssign(self):
+    try:
+      style.SetGlobalStyle(
+        style.CreateStyleFromConfig(
+          '{based_on_style: pep8, '
+          'SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN: True}'))
+      unformatted_code = textwrap.dedent("""\
+      f(a=5)
+      """)
+      expected_formatted_code = textwrap.dedent("""\
+      f(a = 5)
+      """)
+      uwlines = _ParseAndUnwrap(unformatted_code)
+      self.assertCodeEqual(expected_formatted_code,
+                           reformatter.Reformat(uwlines))
+    finally:
+      style.SetGlobalStyle(style.CreatePEP8Style())
+
   def testAsyncWithPrecedingComment(self):
     if sys.version_info[1] < 5:
       return