From e912c7ff54c392e92a765c5eff0bd2ca3bb05b47 Mon Sep 17 00:00:00 2001 From: Bryan Bugyi Date: Mon, 28 Dec 2020 15:30:23 -0500 Subject: [PATCH 1/1] Fix INTERNAL ERROR caused by removing parens from pointless string (#1888) Fixes #1846. --- src/black/__init__.py | 11 ++++++- tests/data/long_strings__regression.py | 42 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 5f0f897..c7c5d72 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -3271,7 +3271,8 @@ class StringParenStripper(StringTransformer): Requirements: The line contains a string which is surrounded by parentheses and: - - The target string is NOT the only argument to a function call). + - The target string is NOT the only argument to a function call. + - The target string is NOT a "pointless" string. - If the target string contains a PERCENT, the brackets are not preceeded or followed by an operator with higher precedence than PERCENT. @@ -3295,6 +3296,14 @@ class StringParenStripper(StringTransformer): if leaf.type != token.STRING: continue + # If this is a "pointless" string... + if ( + leaf.parent + and leaf.parent.parent + and leaf.parent.parent.type == syms.simple_stmt + ): + continue + # Should be preceded by a non-empty LPAR... if ( not is_valid_index(idx - 1) diff --git a/tests/data/long_strings__regression.py b/tests/data/long_strings__regression.py index 7065b2f..2e7f248 100644 --- a/tests/data/long_strings__regression.py +++ b/tests/data/long_strings__regression.py @@ -375,6 +375,27 @@ RE_THREE_BACKSLASHES = { print(f"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam. {[f'{i}' for i in range(10)]}") x = f"This is a long string which contains an f-expr that should not split {{{[i for i in range(5)]}}}." +# The parens should NOT be removed in this case. +( + "my very long string that should get formatted if I'm careful to make sure it goes" + " over 88 characters which it has now" +) + +# The parens should NOT be removed in this case. +( + "my very long string that should get formatted if I'm careful to make sure it goes over 88 characters which" + " it has now" +) + +# The parens should NOT be removed in this case. +( + "my very long string" + " that should get formatted" + " if I'm careful to make sure" + " it goes over 88 characters which" + " it has now" +) + # output @@ -844,3 +865,24 @@ x = ( "This is a long string which contains an f-expr that should not split" f" {{{[i for i in range(5)]}}}." ) + +# The parens should NOT be removed in this case. +( + "my very long string that should get formatted if I'm careful to make sure it goes" + " over 88 characters which it has now" +) + +# The parens should NOT be removed in this case. +( + "my very long string that should get formatted if I'm careful to make sure it goes" + " over 88 characters which it has now" +) + +# The parens should NOT be removed in this case. +( + "my very long string" + " that should get formatted" + " if I'm careful to make sure" + " it goes over 88 characters which" + " it has now" +) -- 2.39.5