From 25028167ffc6f400dad3cab94c5fd0c161746b9f Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Tue, 16 Aug 2022 17:49:21 -0700 Subject: [PATCH] Also handle tuple in addition to list/set. --- src/black/trans.py | 13 ++++++--- tests/data/preview/comments7.py | 46 +++++++++++++++++++----------- tests/data/preview/long_strings.py | 27 ++++++++++++++++++ 3 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/black/trans.py b/src/black/trans.py index bb6f07e2989..33d3e046455 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -1058,8 +1058,13 @@ def _prefer_paren_wrap_match(LL: List[Leaf]) -> Optional[int]: if LL[0].type != token.STRING: return None - matching_nodes = [syms.listmaker, syms.dictsetmaker, syms.arglist] - # If the string is an immediate child of a list/set literal, or + matching_nodes = [ + syms.listmaker, + syms.dictsetmaker, + syms.testlist_gexp, + syms.arglist, + ] + # If the string is an immediate child of a list/set/tuple literal, or # an argument of a function call... if ( parent_type(LL[0]) in matching_nodes @@ -1641,8 +1646,8 @@ class StringParenWrapper(BaseStringSplitter, CustomSplitMapMixin): OR * The line starts with an "atom" string that prefers to be wrapped in parens. It's preferred to be wrapped when it's is an immediate child of - a list/set literal or an argument of a function call, AND the string is - surrounded by commas (or is the first/last child). + a list/set/tuple literal or an argument of a function call, AND the + string is surrounded by commas (or is the first/last child). Transformations: The chosen string is wrapped in parentheses and then split at the LPAR. diff --git a/tests/data/preview/comments7.py b/tests/data/preview/comments7.py index ca9d7c62b21..ec2dc501d8e 100644 --- a/tests/data/preview/comments7.py +++ b/tests/data/preview/comments7.py @@ -226,39 +226,53 @@ class C: # metadata_version errors. ( {}, - "None is an invalid value for Metadata-Version. Error: This field is" - " required. see" - " https://packaging.python.org/specifications/core-metadata", + ( + "None is an invalid value for Metadata-Version. Error: This field" + " is required. see" + " https://packaging.python.org/specifications/core-metadata" + ), ), ( {"metadata_version": "-1"}, - "'-1' is an invalid value for Metadata-Version. Error: Unknown Metadata" - " Version see" - " https://packaging.python.org/specifications/core-metadata", + ( + "'-1' is an invalid value for Metadata-Version. Error: Unknown" + " Metadata Version see" + " https://packaging.python.org/specifications/core-metadata" + ), ), # name errors. ( {"metadata_version": "1.2"}, - "'' is an invalid value for Name. Error: This field is required. see" - " https://packaging.python.org/specifications/core-metadata", + ( + "'' is an invalid value for Name. Error: This field is required." + " see https://packaging.python.org/specifications/core-metadata" + ), ), ( {"metadata_version": "1.2", "name": "foo-"}, - "'foo-' is an invalid value for Name. Error: Must start and end with a" - " letter or numeral and contain only ascii numeric and '.', '_' and" - " '-'. see https://packaging.python.org/specifications/core-metadata", + ( + "'foo-' is an invalid value for Name. Error: Must start and end" + " with a letter or numeral and contain only ascii numeric and '.'," + " '_' and '-'. see" + " https://packaging.python.org/specifications/core-metadata" + ), ), # version errors. ( {"metadata_version": "1.2", "name": "example"}, - "'' is an invalid value for Version. Error: This field is required. see" - " https://packaging.python.org/specifications/core-metadata", + ( + "'' is an invalid value for Version. Error: This field is required." + " see https://packaging.python.org/specifications/core-metadata" + ), ), ( {"metadata_version": "1.2", "name": "example", "version": "dog"}, - "'dog' is an invalid value for Version. Error: Must start and end with" - " a letter or numeral and contain only ascii numeric and '.', '_' and" - " '-'. see https://packaging.python.org/specifications/core-metadata", + ( + "'dog' is an invalid value for Version. Error: Must start and end" + " with a letter or numeral and contain only ascii numeric and '.'," + " '_' and '-'. see" + " https://packaging.python.org/specifications/core-metadata" + ), ), ], ) diff --git a/tests/data/preview/long_strings.py b/tests/data/preview/long_strings.py index 6ab2510227f..6347fd4d60b 100644 --- a/tests/data/preview/long_strings.py +++ b/tests/data/preview/long_strings.py @@ -26,6 +26,10 @@ S2 = {"This is a really long string that can't be expected to fit in one line and is the only child of a set literal."} +T1 = ("The is a short string", "This is a really long string that can't possibly be expected to fit all together on one line. Also it is inside a tuple literal, so it's expected to be wrapped in parens when spliting to avoid implicit str concatenation.", short_call("arg", {"key": "value"}), "This is another really really (not really) long string that also can't be expected to fit on one line and is, like the other string, inside a tuple literal.", ("parens should be stripped for short string in list")) + +T2 = ("This is a really long string that can't be expected to fit in one line and is the only child of a tuple literal.",) + func_with_keywords(my_arg, my_kwarg="Long keyword strings also need to be wrapped, but they will probably need to be handled a little bit differently.") bad_split1 = ( @@ -397,6 +401,29 @@ def foo(): " only child of a set literal." } +T1 = ( + "The is a short string", + ( + "This is a really long string that can't possibly be expected to fit all" + " together on one line. Also it is inside a tuple literal, so it's expected to" + " be wrapped in parens when spliting to avoid implicit str concatenation." + ), + short_call("arg", {"key": "value"}), + ( + "This is another really really (not really) long string that also can't be" + " expected to fit on one line and is, like the other string, inside a tuple" + " literal." + ), + "parens should be stripped for short string in list", +) + +T2 = ( + ( + "This is a really long string that can't be expected to fit in one line and is" + " the only child of a tuple literal." + ), +) + func_with_keywords( my_arg, my_kwarg=(