Skip to content

Commit

Permalink
Allow subscript splitting around and/or ops
Browse files Browse the repository at this point in the history
The LHS and RHS of the ops are still left contiguous, but loosen the
restriction when it's and/or ops.

Closes #332
  • Loading branch information
bwendling committed Jan 25, 2017
1 parent bf63118 commit 3ac87fe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
split here:

func(a, b, c, d(x, y, z=42))
- Allow splitting inside a subscript if it's a logical or bitwise operating.
This should keep the subscript mostly contiguous otherwise.

## [0.15.1] 2017-01-21
### Fixed
Expand Down
25 changes: 22 additions & 3 deletions yapf/yapflib/split_penalty.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,34 @@ def Visit_trailer(self, node): # pylint: disable=invalid-name
pytree_utils.SetNodeAnnotation(
_FirstChildNode(node.children[1]),
pytree_utils.Annotation.SPLIT_PENALTY, ONE_ELEMENT_ARGUMENT)
elif (pytree_utils.NodeName(node.children[0]) == 'LSQB' and
(name.endswith('_test') or name.endswith('_expr'))):
self._SetStronglyConnected(node.children[1].children[0])
self._SetStronglyConnected(node.children[1].children[2])

# Still allow splitting around the operator.
split_before = (
(name.endswith('_test') and
style.Get('SPLIT_BEFORE_LOGICAL_OPERATOR')) or
(name.endswith('_expr') and
style.Get('SPLIT_BEFORE_BITWISE_OPERATOR')))
if split_before:
pytree_utils.SetNodeAnnotation(
_LastChildNode(node.children[1].children[1]),
pytree_utils.Annotation.SPLIT_PENALTY, 0)
else:
pytree_utils.SetNodeAnnotation(
_FirstChildNode(node.children[1].children[2]),
pytree_utils.Annotation.SPLIT_PENALTY, 0)

# Don't split the ending bracket of a subscript list.
self._SetVeryStronglyConnected(node.children[-1])
elif name not in {
'arglist', 'argument', 'term', 'or_test', 'and_test', 'comparison',
'atom'
}:
# Don't split an argument list with one element if at all possible.
self._SetStronglyConnected(node.children[1], node.children[2])
if pytree_utils.NodeName(node.children[-1]) == 'RSQB':
# Don't split the ending bracket of a subscript list.
self._SetVeryStronglyConnected(*node.children)

def Visit_power(self, node): # pylint: disable=invalid-name,missing-docstring
# power ::= atom trailer* ['**' factor]
Expand Down
13 changes: 13 additions & 0 deletions yapftests/reformatter_pep8_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ def testSplittingBeforeFirstArgument(self):
finally:
style.SetGlobalStyle(style.CreatePEP8Style())

def testSplittingExpressionsInsideSubscripts(self):
unformatted_code = textwrap.dedent("""\
def foo():
df = df[(df['campaign_status'] == 'LIVE') & (df['action_status'] == 'LIVE')]
""")
expected_formatted_code = textwrap.dedent("""\
def foo():
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))


if __name__ == '__main__':
unittest.main()

0 comments on commit 3ac87fe

Please sign in to comment.