Skip to content

Commit ccea10e

Browse files
committedNov 9, 2016
Encompass the dictionary value in pseudo parens
If a dictionary's value has a comment before it, then we want to include the value within the pseudo parens. Otherwise only the comment is seen as being part of the dictionary's value.
1 parent 6041f22 commit ccea10e

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed
 

‎CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
## [0.13.3] UNRELEASED
66
### Fixed
77
- "not in" and "is not" should be subtyped as binary operators.
8+
- A non-Node dictionary value may have a comment before it. In those cases, we
9+
want to avoid encompassing only the comment in pseudo parens. So we include
10+
the actual value as well.
811

912
## [0.13.2] 2016-10-22
1013
### Fixed

‎yapf/yapflib/comment_splicer.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,17 @@ def _VisitNodeRec(node):
176176
if comment_lineno == prev_leaf[0].lineno:
177177
comment_lines = comment_prefix.splitlines()
178178
value = comment_lines[0].lstrip()
179-
comment_column = prev_leaf[0].column + len(prev_leaf[0].value)
180-
comment_column += (
181-
len(comment_lines[0]) - len(comment_lines[0].lstrip()))
182-
comment_leaf = pytree.Leaf(
183-
type=token.COMMENT,
184-
value=value.rstrip('\n'),
185-
context=('', (comment_lineno, comment_column)))
186-
pytree_utils.InsertNodesAfter([comment_leaf], prev_leaf[0])
187-
comment_prefix = '\n'.join(comment_lines[1:])
188-
comment_lineno += 1
179+
if value.rstrip('\n'):
180+
comment_column = prev_leaf[0].column + len(prev_leaf[0].value)
181+
comment_column += (
182+
len(comment_lines[0]) - len(comment_lines[0].lstrip()))
183+
comment_leaf = pytree.Leaf(
184+
type=token.COMMENT,
185+
value=value.rstrip('\n'),
186+
context=('', (comment_lineno, comment_column)))
187+
pytree_utils.InsertNodesAfter([comment_leaf], prev_leaf[0])
188+
comment_prefix = '\n'.join(comment_lines[1:])
189+
comment_lineno += 1
189190

190191
rindex = (0 if '\n' not in comment_prefix.rstrip() else
191192
comment_prefix.rstrip().rindex('\n') + 1)

‎yapf/yapflib/subtype_assigner.py

+12
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,18 @@ def _InsertPseudoParentheses(node):
347347
first = _GetFirstLeafNode(node)
348348
last = _GetLastLeafNode(node)
349349

350+
if first == last and first.type == token.COMMENT:
351+
# A comment was inserted before the value, which is a pytree.Leaf.
352+
# Encompass the dictionary's value into an ATOM node.
353+
last = first.next_sibling
354+
new_node = pytree.Node(syms.atom, [first.clone(), last.clone()])
355+
node.replace(new_node)
356+
node = new_node
357+
last.remove()
358+
359+
first = _GetFirstLeafNode(node)
360+
last = _GetLastLeafNode(node)
361+
350362
lparen = pytree.Leaf(
351363
token.LPAR, u'(', context=('', (first.get_lineno(), first.column - 1)))
352364
last_lineno = last.get_lineno()

‎yapftests/reformatter_buganizer_test.py

+17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ class BuganizerFixes(yapf_test_helper.YAPFTest):
2828
def setUpClass(cls):
2929
style.SetGlobalStyle(style.CreateChromiumStyle())
3030

31+
def testB32737279(self):
32+
unformatted_code = textwrap.dedent("""\
33+
here_is_a_dict = {
34+
'key':
35+
# Comment.
36+
'value'
37+
}
38+
""")
39+
expected_formatted_code = textwrap.dedent("""\
40+
here_is_a_dict = {
41+
'key': # Comment.
42+
'value'
43+
}
44+
""")
45+
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
46+
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
47+
3148
def testB32570937(self):
3249
code = textwrap.dedent("""\
3350
def _():

0 commit comments

Comments
 (0)
Please sign in to comment.