Skip to content

Commit

Permalink
Update PEP 8 style to match modern PEP 8
Browse files Browse the repository at this point in the history
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
python/peps@c59c437

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.
  • Loading branch information
njsmith committed Jun 15, 2017
1 parent 5683a4c commit e3b1e9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 6 additions & 2 deletions yapf/yapflib/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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


Expand All @@ -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


Expand Down
12 changes: 6 additions & 6 deletions yapftests/reformatter_pep8_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def g():
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
""")
Expand Down Expand Up @@ -205,8 +205,8 @@ def h():
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)):
Expand Down Expand Up @@ -303,8 +303,8 @@ def foo():
""")
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))
Expand Down

0 comments on commit e3b1e9f

Please sign in to comment.