diff --git a/CHANGELOG b/CHANGELOG index 3fcdd59e9..9a0104e1f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 @@ -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. diff --git a/yapf/yapflib/split_penalty.py b/yapf/yapflib/split_penalty.py index 9960c3380..cef83d3e9 100644 --- a/yapf/yapflib/split_penalty.py +++ b/yapf/yapflib/split_penalty.py @@ -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 @@ -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: diff --git a/yapf/yapflib/subtype_assigner.py b/yapf/yapflib/subtype_assigner.py index 48a9bed92..4bfc1b290 100644 --- a/yapf/yapflib/subtype_assigner.py +++ b/yapf/yapflib/subtype_assigner.py @@ -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) diff --git a/yapftests/reformatter_python3_test.py b/yapftests/reformatter_python3_test.py index 9ddf4dc45..dcc2d6458 100644 --- a/yapftests/reformatter_python3_test.py +++ b/yapftests/reformatter_python3_test.py @@ -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()