Don't count pseudo-parens in the length of the line.
diff --git a/CHANGELOG b/CHANGELOG
index 1a9b7b3..a79f16c 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -14,6 +14,7 @@
line length.
- Don't count a dictionary entry as not fitting on a single line in a
dictionary.
+- Don't count pseudo-parentheses in the length of the line.
## [0.13.2] 2016-10-22
### Fixed
diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index a785ba2..32493e7 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -508,7 +508,7 @@
if is_multiline_string:
# This is a multiline string. Only look at the first line.
self.column += len(current.value.split('\n')[0])
- elif not current.is_pseudo_paren or current.value == '(':
+ elif not current.is_pseudo_paren:
self.column += len(current.value)
self.next_token = self.next_token.next_token
@@ -528,8 +528,10 @@
def _FitsOnLine(self, start, end):
"""Determines if line between start and end can fit on the current line."""
- length = end.total_length - start.total_length + len(start.value)
- return length + self.column < self.column_limit
+ length = end.total_length - start.total_length
+ if not start.is_pseudo_paren:
+ length += len(start.value)
+ return length + self.column <= self.column_limit
def _EachDictEntryFitsOnOneLine(self, opening):
"""Determine if each dict elems can fit on one line."""
diff --git a/yapf/yapflib/subtype_assigner.py b/yapf/yapflib/subtype_assigner.py
index 55b7058..f7223a8 100644
--- a/yapf/yapflib/subtype_assigner.py
+++ b/yapf/yapflib/subtype_assigner.py
@@ -131,9 +131,8 @@
# comp_op ::= '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not in'|'is'|'is not'
for child in node.children:
self.Visit(child)
- if (isinstance(child, pytree.Leaf) and child.value in {
- '<', '>', '==', '>=', '<=', '<>', '!=', 'in', 'is'
- }):
+ if (isinstance(child, pytree.Leaf) and
+ child.value in {'<', '>', '==', '>=', '<=', '<>', '!=', 'in', 'is'}):
_AppendTokenSubtype(child, format_token.Subtype.BINARY_OPERATOR)
elif pytree_utils.NodeName(child) == 'comp_op':
for grandchild in child.children:
diff --git a/yapf/yapflib/unwrapped_line.py b/yapf/yapflib/unwrapped_line.py
index 0f419d1..712dabe 100644
--- a/yapf/yapflib/unwrapped_line.py
+++ b/yapf/yapflib/unwrapped_line.py
@@ -194,7 +194,10 @@
# Space between keyword... tokens and pseudo parens.
return True
if left.is_pseudo_paren or right.is_pseudo_paren:
- # The pseudo-parens shouldn't affect spacing.
+ # There should be a space after the ':' in a dictionary.
+ if left.OpensScope():
+ return True
+ # The closing pseudo-paren shouldn't affect spacing.
return False
if left.is_continuation or right.is_continuation:
# The continuation node's value has all of the spaces it needs.
diff --git a/yapftests/pytree_utils_test.py b/yapftests/pytree_utils_test.py
index 8861a40..161c939 100644
--- a/yapftests/pytree_utils_test.py
+++ b/yapftests/pytree_utils_test.py
@@ -188,8 +188,7 @@
self.assertSetEqual(
pytree_utils.GetNodeAnnotation(self._leaf,
- pytree_utils.Annotation.SUBTYPE),
- {_FOO})
+ pytree_utils.Annotation.SUBTYPE), {_FOO})
pytree_utils.RemoveSubtypeAnnotation(self._leaf, _FOO)
diff --git a/yapftests/reformatter_buganizer_test.py b/yapftests/reformatter_buganizer_test.py
index 1ea8c89..58fb261 100644
--- a/yapftests/reformatter_buganizer_test.py
+++ b/yapftests/reformatter_buganizer_test.py
@@ -28,6 +28,20 @@
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())
+ def testB33047408(self):
+ code = textwrap.dedent("""\
+ def _():
+ for sort in (sorts or []):
+ request['sorts'].append({
+ 'field': {
+ 'user_field': sort
+ },
+ 'order': 'ASCENDING'
+ })
+ """)
+ uwlines = yapf_test_helper.ParseAndUnwrap(code)
+ self.assertCodeEqual(code, reformatter.Reformat(uwlines))
+
def testB32714745(self):
code = textwrap.dedent("""\
class _():
@@ -701,7 +715,7 @@
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
def testB20849933(self):
- code = textwrap.dedent("""\
+ unformatted_code = textwrap.dedent("""\
def main(unused_argv):
if True:
aaaaaaaa = {
@@ -709,8 +723,16 @@
(eeeeee.FFFFFFFFFFFFFFFFFF),
}
""")
- uwlines = yapf_test_helper.ParseAndUnwrap(code)
- self.assertCodeEqual(code, reformatter.Reformat(uwlines))
+ expected_formatted_code = textwrap.dedent("""\
+ def main(unused_argv):
+ if True:
+ aaaaaaaa = {
+ 'xxx':
+ '%s/cccccc/ddddddddddddddddddd.jar' % (eeeeee.FFFFFFFFFFFFFFFFFF),
+ }
+ """)
+ uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
def testB20813997(self):
code = textwrap.dedent("""\
diff --git a/yapftests/reformatter_facebook_test.py b/yapftests/reformatter_facebook_test.py
index a4e88a9..7ed2631 100644
--- a/yapftests/reformatter_facebook_test.py
+++ b/yapftests/reformatter_facebook_test.py
@@ -317,9 +317,7 @@
def _():
effect_line = FrontInput(
effect_line_offset, line_content,
- LineSource(
- 'localhost', xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- )
+ LineSource('localhost', xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
)
""")
uwlines = yapf_test_helper.ParseAndUnwrap(code)