From 23a00f051576d2e7edd18b6af382902cc34ea4a2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Langa?= Date: Mon, 4 Jun 2018 20:24:50 -0700 Subject: [PATCH] Fix long trivial assignments being wrapped in unnecessary parentheses Fixes #273 --- README.md | 2 ++ black.py | 15 +++++++++++---- tests/cantfit.py | 4 ++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 911d1f9..bfd77f9 100644 --- a/README.md +++ b/README.md @@ -719,6 +719,8 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md). * the header output in `--diff` now actually conforms to the unified diff spec +* fixed long trivial assignments being wrapped in unnecessary parentheses (#273) + * fixed stdin handling not working correctly if an old version of Click was used (#276) diff --git a/black.py b/black.py index c6b018f..35af598 100644 --- a/black.py +++ b/black.py @@ -2213,11 +2213,14 @@ def right_hand_split( result.append(leaf, preformatted=True) for comment_after in line.comments_after(leaf): result.append(comment_after, preformatted=True) - bracket_split_succeeded_or_raise(head, body, tail) assert opening_bracket and closing_bracket + body.should_explode = should_explode(body, opening_bracket) + bracket_split_succeeded_or_raise(head, body, tail) if ( + # the body shouldn't be exploded + not body.should_explode # the opening bracket is an optional paren - opening_bracket.type == token.LPAR + and opening_bracket.type == token.LPAR and not opening_bracket.value # the closing bracket is an optional paren and closing_bracket.type == token.RPAR @@ -2234,11 +2237,15 @@ def right_hand_split( yield from right_hand_split(line, line_length, py36=py36, omit=omit) return except CannotSplit: - pass + if len(body.leaves) == 1 and not is_line_short_enough( + body, line_length=line_length + ): + raise CannotSplit( + "Splitting failed, body is still too long and can't be split." + ) ensure_visible(opening_bracket) ensure_visible(closing_bracket) - body.should_explode = should_explode(body, opening_bracket) for result in (head, body, tail): if result: yield result diff --git a/tests/cantfit.py b/tests/cantfit.py index bc38ed2..816cdef 100644 --- a/tests/cantfit.py +++ b/tests/cantfit.py @@ -25,6 +25,9 @@ normal_name = normal_function_name( "eggs with spam and eggs and spam with eggs with spam and eggs and spam with eggs with spam and eggs and spam with eggs", this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it=0, ) +string_variable_name = ( + "a string that is waaaaaaaayyyyyyyy too long, even in parens, there's nothing you can do" # noqa +) # output @@ -67,3 +70,4 @@ normal_name = normal_function_name( "eggs with spam and eggs and spam with eggs with spam and eggs and spam with eggs with spam and eggs and spam with eggs", this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it=0, ) +string_variable_name = "a string that is waaaaaaaayyyyyyyy too long, even in parens, there's nothing you can do" # noqa -- 2.39.5