Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Increase split penalty around exponent operator
  • Loading branch information
bwendling committed Dec 5, 2018
1 parent d1a1b3b commit 05706ac
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -25,6 +25,7 @@
dictionary entry is a container. If so, then it's probably split over
multiple lines with the opening bracket on the same line as the key.
Therefore, we shouldn't enforce a split because of that.
- Increase split penalty around exponent operator.

## [0.25.0] 2018-11-25
### Added
Expand Down
16 changes: 7 additions & 9 deletions yapf/yapflib/split_penalty.py
Expand Up @@ -144,41 +144,39 @@ def Visit_parameters(self, node): # pylint: disable=invalid-name
def Visit_arglist(self, node): # pylint: disable=invalid-name
# arglist ::= argument (',' argument)* [',']
self.DefaultNodeVisit(node)
index = 1
while index < len(node.children):

for index in py3compat.range(1, len(node.children)):
child = node.children[index]
if isinstance(child, pytree.Leaf) and child.value == ',':
_SetUnbreakable(child)
index += 1

for child in node.children:
if pytree_utils.NodeName(child) == 'atom':
_IncreasePenalty(child, CONNECTED)

def Visit_argument(self, node): # pylint: disable=invalid-name
# argument ::= test [comp_for] | test '=' test # Really [keyword '='] test
self.DefaultNodeVisit(node)
index = 1
while index < len(node.children) - 1:

for index in py3compat.range(1, len(node.children) - 1):
child = node.children[index]
if isinstance(child, pytree.Leaf) and child.value == '=':
_SetSplitPenalty(
pytree_utils.FirstLeafNode(node.children[index]), NAMED_ASSIGN)
_SetSplitPenalty(
pytree_utils.FirstLeafNode(node.children[index + 1]), NAMED_ASSIGN)
index += 1

def Visit_tname(self, node): # pylint: disable=invalid-name
# tname ::= NAME [':' test]
self.DefaultNodeVisit(node)
index = 1
while index < len(node.children) - 1:

for index in py3compat.range(1, len(node.children) - 1):
child = node.children[index]
if isinstance(child, pytree.Leaf) and child.value == ':':
_SetSplitPenalty(
pytree_utils.FirstLeafNode(node.children[index]), NAMED_ASSIGN)
_SetSplitPenalty(
pytree_utils.FirstLeafNode(node.children[index + 1]), NAMED_ASSIGN)
index += 1

def Visit_dotted_name(self, node): # pylint: disable=invalid-name
# dotted_name ::= NAME ('.' NAME)*
Expand Down
4 changes: 3 additions & 1 deletion yapf/yapflib/unwrapped_line.py
Expand Up @@ -528,7 +528,9 @@ def _SplitPenalty(prev_token, cur_token):
return 0
if prev_token.is_binary_op:
# We would rather not split after an equality operator.
return 20
return split_penalty.CONNECTED
if pval == '**' or cval == '**':
return split_penalty.STRONGLY_CONNECTED
if (format_token.Subtype.VARARGS_STAR in prev_token.subtypes or
format_token.Subtype.KWARGS_STAR_STAR in prev_token.subtypes):
# Don't split after a varargs * or kwargs **.
Expand Down
10 changes: 10 additions & 0 deletions yapftests/reformatter_buganizer_test.py
Expand Up @@ -28,6 +28,16 @@ class BuganizerFixes(yapf_test_helper.YAPFTest):
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())

def testB73166511(self):
code = """\
def _():
if min_std is not None:
groundtruth_age_variances = tf.maximum(groundtruth_age_variances,
min_std**2)
"""
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))

def testB118624921(self):
code = """\
def _():
Expand Down

0 comments on commit 05706ac

Please sign in to comment.