diff --git a/CHANGELOG b/CHANGELOG index d72274f8b..214f01364 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/yapf/yapflib/split_penalty.py b/yapf/yapflib/split_penalty.py index 9279a36f1..7d84d8a3d 100644 --- a/yapf/yapflib/split_penalty.py +++ b/yapf/yapflib/split_penalty.py @@ -144,12 +144,12 @@ 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) @@ -157,28 +157,26 @@ def Visit_arglist(self, node): # pylint: disable=invalid-name 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)* diff --git a/yapf/yapflib/unwrapped_line.py b/yapf/yapflib/unwrapped_line.py index 44bdc3145..40f129571 100644 --- a/yapf/yapflib/unwrapped_line.py +++ b/yapf/yapflib/unwrapped_line.py @@ -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 **. diff --git a/yapftests/reformatter_buganizer_test.py b/yapftests/reformatter_buganizer_test.py index 8f353d8e6..38e4437f1 100644 --- a/yapftests/reformatter_buganizer_test.py +++ b/yapftests/reformatter_buganizer_test.py @@ -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 _():