Skip to content

Commit

Permalink
Keep type annotations intact if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
bwendling committed Jan 13, 2017
1 parent 10e391a commit 61061f6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG
Expand Up @@ -2,7 +2,10 @@
# All notable changes to this project will be documented in this file.
# This project adheres to [Semantic Versioning](http://semver.org/).

## [0.14.1] UNRELEASED
## [0.15.0] 2017-01-12
### Added
- Keep type annotations intact as much as possible. Don't try to split the over
mutliple lines.
### Fixed
- When determining if each element in a dictionary can fit on a single line, we
are skipping dictionary entries. However, we need to ignore comments in our
Expand All @@ -12,7 +15,7 @@
- Also allow text before a "yapf: (disable|enable)" comment.

## [0.14.0] 2016-11-21
#### Added
### Added
- formatting can be run in parallel using the "-p" / "--parallel" flags.
### Fixed
- "not in" and "is not" should be subtyped as binary operators.
Expand Down
17 changes: 13 additions & 4 deletions yapf/yapflib/split_penalty.py
Expand Up @@ -82,13 +82,22 @@ def Visit_funcdef(self, node): # pylint: disable=invalid-name
while pytree_utils.NodeName(node.children[colon_idx]) == 'simple_stmt':
colon_idx += 1
self._SetUnbreakable(node.children[colon_idx])
arrow_idx = -1
while colon_idx < len(node.children):
if (isinstance(node.children[colon_idx], pytree.Leaf) and
node.children[colon_idx].value == ':'):
break
if isinstance(node.children[colon_idx], pytree.Leaf):
if node.children[colon_idx].value == ':':
break
if node.children[colon_idx].value == '->':
arrow_idx = colon_idx
colon_idx += 1
self._SetUnbreakable(node.children[colon_idx])
self.DefaultNodeVisit(node)
if arrow_idx > 0:
pytree_utils.SetNodeAnnotation(
_LastChildNode(node.children[arrow_idx - 1]),
pytree_utils.Annotation.SPLIT_PENALTY, 0)
self._SetUnbreakable(node.children[arrow_idx])
self._SetStronglyConnected(node.children[arrow_idx + 1])

def Visit_lambdef(self, node): # pylint: disable=invalid-name
# lambdef ::= 'lambda' [varargslist] ':' test
Expand Down Expand Up @@ -317,7 +326,7 @@ def _SetUnbreakableOnChildren(self, node):
self._SetUnbreakable(node.children[i])

def _SetExpressionPenalty(self, node, penalty):
"""Set an ARITHMETIC_EXPRESSION penalty annotation children nodes."""
"""Set a penalty annotation on children nodes."""

def RecArithmeticExpression(node, first_child_leaf):
if node is first_child_leaf:
Expand Down
3 changes: 2 additions & 1 deletion yapf/yapflib/subtype_assigner.py
Expand Up @@ -83,7 +83,8 @@ def Visit_dictsetmaker(self, node): # pylint: disable=invalid-name
for child in node.children:
if dict_maker:
if pytree_utils.NodeName(child) == 'DOUBLESTAR':
_AppendFirstLeafTokenSubtype(child, format_token.Subtype.KWARGS_STAR_STAR)
_AppendFirstLeafTokenSubtype(child,
format_token.Subtype.KWARGS_STAR_STAR)
if last_was_colon:
if style.Get('INDENT_DICTIONARY_VALUE'):
_InsertPseudoParentheses(child)
Expand Down
18 changes: 18 additions & 0 deletions yapftests/reformatter_python3_test.py
Expand Up @@ -179,6 +179,24 @@ async def foo():
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))

def testKeepTypesIntact(self):
if sys.version_info[1] < 5:
return
unformatted_code = textwrap.dedent("""\
def _ReduceAbstractContainers(
self, *args: Optional[automation_converter.PyiCollectionAbc]) -> List[
automation_converter.PyiCollectionAbc]:
pass
""")
expected_formatted_code = textwrap.dedent("""\
def _ReduceAbstractContainers(
self, *args: Optional[automation_converter.PyiCollectionAbc]
) -> List[automation_converter.PyiCollectionAbc]:
pass
""")
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))


if __name__ == '__main__':
unittest.main()

0 comments on commit 61061f6

Please sign in to comment.