Skip to content

Commit

Permalink
Also handle tuple in addition to list/set.
Browse files Browse the repository at this point in the history
  • Loading branch information
yilei committed Aug 23, 2022
1 parent 25f7e49 commit 2502816
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 20 deletions.
13 changes: 9 additions & 4 deletions src/black/trans.py
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
46 changes: 30 additions & 16 deletions tests/data/preview/comments7.py
Expand Up @@ -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"
),
),
],
)
Expand Down
27 changes: 27 additions & 0 deletions tests/data/preview/long_strings.py
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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=(
Expand Down

0 comments on commit 2502816

Please sign in to comment.