Look for pylint comments disabling long lambda warnings This allows long lambdas on a case-by-case basis.
diff --git a/CHANGELOG b/CHANGELOG index 8e461a9..0a00c8c 100644 --- a/CHANGELOG +++ b/CHANGELOG
@@ -12,6 +12,9 @@ without wrapping. If not, then we split. - Check all of the elements of a tuple. Similarly to how arguments are analyzed. This allows tuples to be split more rationally. +### Fixed +- Attempt to determine if long lambdas are allowed. This can be done on a + case-by-case basis with a "pylint" disable comment. ## [0.21.0] 2018-03-18 ### Added
diff --git a/yapf/yapflib/split_penalty.py b/yapf/yapflib/split_penalty.py index 9fae327..66f733f 100644 --- a/yapf/yapflib/split_penalty.py +++ b/yapf/yapflib/split_penalty.py
@@ -13,6 +13,8 @@ # limitations under the License. """Computation of split penalties before/between tokens.""" +import re + from lib2to3 import pytree from yapf.yapflib import format_token @@ -111,7 +113,15 @@ def Visit_lambdef(self, node): # pylint: disable=invalid-name # lambdef ::= 'lambda' [varargslist] ':' test # Loop over the lambda up to and including the colon. - if style.Get('ALLOW_MULTILINE_LAMBDAS'): + allow_multiline_lambdas = style.Get('ALLOW_MULTILINE_LAMBDAS') + if not allow_multiline_lambdas: + for child in node.children: + if pytree_utils.NodeName(child) == 'COMMENT': + if re.search(r'pylint:.*disable=.*\bg-long-lambda', child.value): + allow_multiline_lambdas = True + break + + if allow_multiline_lambdas: _SetStronglyConnected(node) else: self._SetUnbreakableOnChildren(node)
diff --git a/yapftests/reformatter_buganizer_test.py b/yapftests/reformatter_buganizer_test.py index 0261798..5b50c28 100644 --- a/yapftests/reformatter_buganizer_test.py +++ b/yapftests/reformatter_buganizer_test.py
@@ -28,6 +28,27 @@ def setUpClass(cls): style.SetGlobalStyle(style.CreateChromiumStyle()) + def testB37099651(self): + unformatted_code = """\ +_MEMCACHE = lazy.MakeLazy( + # pylint: disable=g-long-lambda + lambda: function.call.mem.clients(FLAGS.some_flag_thingy, default_namespace=_LAZY_MEM_NAMESPACE, allow_pickle=True) + # pylint: enable=g-long-lambda +) +""" + expected_formatted_code = """\ +_MEMCACHE = lazy.MakeLazy( + # pylint: disable=g-long-lambda + lambda: function.call.mem.clients( + FLAGS.some_flag_thingy, + default_namespace=_LAZY_MEM_NAMESPACE, + allow_pickle=True) + # pylint: enable=g-long-lambda +) +""" + uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + def testB33228502(self): unformatted_code = """\ def _():