Skip to content

Commit

Permalink
Ignore comments when determine what's a dict maker.
Browse files Browse the repository at this point in the history
  • Loading branch information
bwendling committed Oct 21, 2015
1 parent d62f89e commit 81e37b6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Retain proper vertical spacing before comments in a data literal.
- Make sure that continuations from a compound statement are distinguished from
the succeeding line.
- Ignore preceding comments when calculating what is a "dictonary maker".

## [0.6.0] 2015-10-18
### Added
Expand Down
13 changes: 10 additions & 3 deletions yapf/yapflib/subtype_assigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ def Visit_dictsetmaker(self, node): # pylint: disable=invalid-name
# dictsetmaker ::= (test ':' test (comp_for |
# (',' test ':' test)* [','])) |
# (test (comp_for | (',' test)* [',']))
dict_maker = (len(node.children) > 1 and isinstance(node.children[1],
pytree.Leaf) and
node.children[1].value == ':')
dict_maker = False
if len(node.children) > 1:
index = 0
while index < len(node.children):
if pytree_utils.NodeName(node.children[index]) != 'COMMENT':
break
index += 1
if index < len(node.children):
child = node.children[index + 1]
dict_maker = isinstance(child, pytree.Leaf) and child.value == ':'
last_was_comma = False
last_was_colon = False
for child in node.children:
Expand Down
20 changes: 20 additions & 0 deletions yapftests/reformatter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,26 @@ class BuganizerFixes(ReformatterTest):
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())

def testB25136820(self):
unformatted_code = textwrap.dedent("""\
def foo():
return collections.OrderedDict({
# Preceding comment.
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa':
'$bbbbbbbbbbbbbbbbbbbbbbbb',
})
""")
expected_formatted_code = textwrap.dedent("""\
def foo():
return collections.OrderedDict({
# Preceding comment.
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa':
'$bbbbbbbbbbbbbbbbbbbbbbbb',
})
""")
uwlines = _ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))

def testB25131481(self):
unformatted_code = textwrap.dedent("""\
APPARENT_ACTIONS = ('command_type', {
Expand Down

0 comments on commit 81e37b6

Please sign in to comment.