Skip to content

Commit

Permalink
Don't increase split penalty on 'lambda' keyword
Browse files Browse the repository at this point in the history
If a lambda expression is an argument to a function (common), then we
should be able to split before it without hinderance.
  • Loading branch information
bwendling committed Jun 21, 2019
1 parent a3c12ee commit f42b489
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
### Fixed
- `BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=False` wasn't honored because the
number of newlines was erroneously calculated beforehand.
- Lambda expressions shouldn't have an increased split penalty applied to the
'lambda' keyword. This prevents them from being properly formatted when they're
arguments to functions.

## [0.27.0] 2019-04-07
### Added
Expand Down
2 changes: 1 addition & 1 deletion yapf/yapflib/format_decision_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ def _ContainerFitsOnStartLine(self, opening):
Arguments:
opening: (FormatToken) The unwrapped line we're currently processing.
Returns:
True if the container fits on the start line.
"""
Expand Down
4 changes: 2 additions & 2 deletions yapf/yapflib/split_penalty.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ def Visit_lambdef(self, node): # pylint: disable=invalid-name
break

if allow_multiline_lambdas:
_SetStronglyConnected(node)
_SetExpressionPenalty(node, STRONGLY_CONNECTED)
else:
_SetVeryStronglyConnected(node)
_SetExpressionPenalty(node, VERY_STRONGLY_CONNECTED)

def Visit_parameters(self, node): # pylint: disable=invalid-name
# parameters ::= '(' [typedargslist] ')'
Expand Down
11 changes: 11 additions & 0 deletions yapftests/reformatter_buganizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ class BuganizerFixes(yapf_test_helper.YAPFTest):
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())

def testB26521719(self):
code = """\
class _():
def _(self):
self.stubs.Set(some_type_of_arg, 'ThisIsAStringArgument',
lambda *unused_args, **unused_kwargs: fake_resolver)
"""
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))

def testB122541552(self):
code = """\
# pylint: disable=g-explicit-bool-comparison,singleton-comparison
Expand Down
4 changes: 2 additions & 2 deletions yapftests/split_penalty_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class B(A):
""")
tree = self._ParseAndComputePenalties(code)
self._CheckPenalties(tree, [
('lambda', VERY_STRONGLY_CONNECTED),
('lambda', None),
('a', VERY_STRONGLY_CONNECTED),
(',', VERY_STRONGLY_CONNECTED),
('b', VERY_STRONGLY_CONNECTED),
Expand Down Expand Up @@ -180,7 +180,7 @@ def testStronglyConnected(self):
(',', None),
('y', None),
('(', UNBREAKABLE),
('lambda', VERY_STRONGLY_CONNECTED),
('lambda', STRONGLY_CONNECTED),
('a', VERY_STRONGLY_CONNECTED),
(':', VERY_STRONGLY_CONNECTED),
('23', VERY_STRONGLY_CONNECTED),
Expand Down
6 changes: 3 additions & 3 deletions yapftests/subtype_assigner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def testBitwiseOperators(self):
('3', [format_token.Subtype.NONE]),
(')', [format_token.Subtype.NONE]),
('>>', {format_token.Subtype.BINARY_OPERATOR}),
('1', [format_token.Subtype.NONE]), ],
('1', [format_token.Subtype.NONE]),],
]) # yapf: disable

def testArithmeticOperators(self):
Expand Down Expand Up @@ -201,7 +201,7 @@ def testArithmeticOperators(self):
(')', [format_token.Subtype.NONE]),
('//', {format_token.Subtype.BINARY_OPERATOR,
format_token.Subtype.M_EXPR_OPERATOR}),
('1', [format_token.Subtype.NONE]), ],
('1', [format_token.Subtype.NONE]),],
]) # yapf: disable

def testSubscriptColon(self):
Expand Down Expand Up @@ -234,7 +234,7 @@ def testFunctionCallWithStarExpression(self):
('*', {format_token.Subtype.UNARY_OPERATOR,
format_token.Subtype.VARARGS_STAR}),
('b', [format_token.Subtype.NONE]),
(']', [format_token.Subtype.NONE]), ],
(']', [format_token.Subtype.NONE]),],
]) # yapf: disable


Expand Down

0 comments on commit f42b489

Please sign in to comment.