Repository: google/yapf
Pull request #418 (2017-06-15 02:30:43)

Update PEP 8 style to match modern PEP 8
PEP 8 was updated last year to prefer that line splits come before binary operators, rather than after:

https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator

https://github.com/python/peps/commit/c59c4376ad233a62ca4b3a6060c81368bd21e85b

Really there should be a way to do this for other binary operations... for example, even with this setting, YAPF wants to do:

```
     orig_extracted = (
-        extract_tb(orig.__traceback__)
-        + extract_tb(orig.exceptions[0].__traceback__)
-        + extract_tb(orig.exceptions[0].exceptions[1].__traceback__))
+        extract_tb(orig.__traceback__) +
+        extract_tb(orig.exceptions[0].__traceback__) +
+        extract_tb(orig.exceptions[0].exceptions[1].__traceback__))
```

which is exactly what the example in PEP 8 says not to do. But this patch brings YAPF's PEP 8 style closer to what PEP 8 says.

googlebot: Thanks for your pull request.  It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: **Please visit <https://cla.developers.google.com/> to sign.**

Once you've signed, please reply here (e.g. `I signed it!`) and we'll verify.  Thanks.

---

- If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address.  Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).
- If your company signed a CLA, they designated a Point of Contact who decides which employees are authorized to participate. You may need to contact the Point of Contact for your company and ask to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the project maintainer to go/cla#troubleshoot.
- In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again.

<!-- need_sender_cla -->
njsmith: cla signed
googlebot: CLAs look good, thanks!

<!-- ok -->
njsmith: It looks like YAPF's handling of trailing commas is wrong too: PEP 8 says that if you have a trailing comma, the only two good options are to put the final bracket on a new line, or else to write a single element tuple like `(None,)` with no trailing space ([ref](https://github.com/python/peps/commit/c78ccb3dcb52c15b5177f19fa92fa97750e11b70)).

I'm not entirely sure how to express this in YAPF terms -- maybe `SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET` should be `False`, and `SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED` should be `True`? (That's the opposite of how they're set now.)

Patch:
diff --git a/yapf/yapflib/style.py b/yapf/yapflib/style.py
index a3d59ed..762b05a 100644
--- a/yapf/yapflib/style.py
+++ b/yapf/yapflib/style.py
@@ -219,10 +219,10 @@ def CreatePEP8Style():
       SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN=False,
       SPACES_BEFORE_COMMENT=2,
       SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED=False,
-      SPLIT_BEFORE_BITWISE_OPERATOR=False,
+      SPLIT_BEFORE_BITWISE_OPERATOR=True,
       SPLIT_BEFORE_DICT_SET_GENERATOR=True,
       SPLIT_BEFORE_FIRST_ARGUMENT=False,
-      SPLIT_BEFORE_LOGICAL_OPERATOR=False,
+      SPLIT_BEFORE_LOGICAL_OPERATOR=True,
       SPLIT_BEFORE_NAMED_ASSIGNS=True,
       SPLIT_PENALTY_AFTER_OPENING_BRACKET=30,
       SPLIT_PENALTY_AFTER_UNARY_OPERATOR=10000,
@@ -244,6 +244,8 @@ def CreateGoogleStyle():
   style['I18N_COMMENT'] = r'#\..*'
   style['I18N_FUNCTION_CALL'] = ['N_', '_']
   style['SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET'] = False
+  style['SPLIT_BEFORE_LOGICAL_OPERATOR'] = False
+  style['SPLIT_BEFORE_BITWISE_OPERATOR'] = False
   return style
 
 
@@ -267,6 +269,8 @@ def CreateFacebookStyle():
   style['SPLIT_PENALTY_AFTER_OPENING_BRACKET'] = 0
   style['SPLIT_PENALTY_BEFORE_IF_EXPR'] = 30
   style['SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT'] = 30
+  style['SPLIT_BEFORE_LOGICAL_OPERATOR'] = False
+  style['SPLIT_BEFORE_BITWISE_OPERATOR'] = False
   return style
 
 


Test patch:
diff --git a/yapftests/reformatter_pep8_test.py b/yapftests/reformatter_pep8_test.py
index 20fe6b5..431ff99 100644
--- a/yapftests/reformatter_pep8_test.py
+++ b/yapftests/reformatter_pep8_test.py
@@ -168,8 +168,8 @@ class TestsForPEP8Style(yapf_test_helper.YAPFTest):
     expected_formatted_code = textwrap.dedent("""\
         def f():
             def g():
-                while (xxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz]) == 'aaaaaaaaaaa' and
-                       xxxxxxxxxxxxxxxxxxxx(
+                while (xxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz]) == 'aaaaaaaaaaa'
+                       and xxxxxxxxxxxxxxxxxxxx(
                            yyyyyyyyyyyyy[zzzzz].aaaaaaaa[0]) == 'bbbbbbb'):
                     pass
         """)
@@ -205,8 +205,8 @@ class TestsForPEP8Style(yapf_test_helper.YAPFTest):
                 dosomething(connection)
         """)
     expected_formatted_code = textwrap.dedent("""\
-        if (aaaaaaaaaaaaaa + bbbbbbbbbbbbbbbb == ccccccccccccccccc and xxxxxxxxxxxxx or
-                yyyyyyyyyyyyyyyyy):
+        if (aaaaaaaaaaaaaa + bbbbbbbbbbbbbbbb == ccccccccccccccccc and xxxxxxxxxxxxx
+                or yyyyyyyyyyyyyyyyy):
             pass
         elif (xxxxxxxxxxxxxxx(
                 aaaaaaaaaaa, bbbbbbbbbbbbbb, cccccccccccc, dddddddddd=None)):
@@ -303,8 +303,8 @@ class TestsForPEP8Style(yapf_test_helper.YAPFTest):
         """)
     expected_formatted_code = textwrap.dedent("""\
         def foo():
-            df = df[(df['campaign_status'] == 'LIVE') &
-                    (df['action_status'] == 'LIVE')]
+            df = df[(df['campaign_status'] == 'LIVE')
+                    & (df['action_status'] == 'LIVE')]
         """)
     uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
     self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))