Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4011: Handle more huggable immediately nested parens/brackets. #4012

Merged
merged 23 commits into from Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
87ae9b4
Fix #4011: Handle more huggable immediately nested parens/brackets.
yilei Nov 1, 2023
d55b39f
Make this comparison more clear, though it doesn't matter in practice.
yilei Nov 1, 2023
09aad71
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2023
80cd469
Format black code.
yilei Nov 1, 2023
0e5b949
Merge branch 'hug' of https://github.com/yilei/black into hug
yilei Nov 1, 2023
fa28b0d
Format again.
yilei Nov 1, 2023
d27b13a
Merge branch 'main' into hug
yilei Nov 1, 2023
c93d20f
Update CHANGES.md
yilei Nov 1, 2023
1678c80
Do not hug if the argument fits in a single line.
yilei Nov 7, 2023
c21f36c
Merge branch 'main' into hug
yilei Nov 7, 2023
b51cef3
Use magic_trailing_comma instead of should_split_rhs for the shortcut.
yilei Nov 7, 2023
ed8a8f0
Merge branch 'hug' of https://github.com/yilei/black into hug
yilei Nov 7, 2023
a044fb4
Merge branch 'main' into hug
yilei Nov 7, 2023
e9346e5
Merge branch 'hug' of https://github.com/yilei/black into hug
yilei Nov 7, 2023
e3df498
Fix crash.
yilei Nov 7, 2023
36d954c
empty commit to re-trigger CI
yilei Nov 7, 2023
b0d01b2
empty commit to re-trigger CI
yilei Nov 7, 2023
51d477e
Merge branch 'main' into hug
yilei Nov 7, 2023
e9fb7ae
empty commit to re-trigger CI
yilei Nov 8, 2023
4333435
Merge branch 'main' into hug
yilei Nov 8, 2023
15c79dc
Merge branch 'main' into hug
yilei Nov 8, 2023
6134969
Revert almost all changes to try if it's my PR causing diff-shades is…
yilei Nov 9, 2023
240c218
Revert "Revert almost all changes to try if it's my PR causing diff-s…
yilei Nov 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGES.md
Expand Up @@ -13,8 +13,8 @@

### Preview style

- Multiline dictionaries and lists that are the sole argument to a function are now
indented less (#3964)
- Multiline tuples, lists, and dictionaries that are the sole argument to a function or
inside another tuple, list, or dictionary are now indented less (#3964, #4012)
yilei marked this conversation as resolved.
Show resolved Hide resolved
- Multiline list and dict unpacking as the sole argument to a function is now also
indented less (#3992)

Expand Down
36 changes: 16 additions & 20 deletions docs/conf.py
Expand Up @@ -149,15 +149,13 @@ def make_pypi_svg(version: str) -> None:
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(
master_doc,
"black.tex",
"Documentation for Black",
"Łukasz Langa and contributors to Black",
"manual",
)
]
latex_documents = [(
master_doc,
"black.tex",
"Documentation for Black",
"Łukasz Langa and contributors to Black",
"manual",
)]


# -- Options for manual page output ------------------------------------------
Expand All @@ -172,17 +170,15 @@ def make_pypi_svg(version: str) -> None:
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(
master_doc,
"Black",
"Documentation for Black",
author,
"Black",
"The uncompromising Python code formatter",
"Miscellaneous",
)
]
texinfo_documents = [(
master_doc,
"Black",
"Documentation for Black",
author,
"Black",
"The uncompromising Python code formatter",
"Miscellaneous",
)]


# -- Options for Epub output -------------------------------------------------
Expand Down
17 changes: 15 additions & 2 deletions docs/the_black_code_style/future_style.md
Expand Up @@ -116,8 +116,7 @@ my_dict = {
### Improved multiline dictionary and list indentation for sole function parameter

For better readability and less verticality, _Black_ now pairs parentheses ("(", ")")
with braces ("{", "}") and square brackets ("[", "]") on the same line for single
parameter function calls. For example:
with braces ("{", "}") and square brackets ("[", "]") on the same line. For example:

```python
foo(
Expand All @@ -127,6 +126,14 @@ foo(
3,
]
)

nested_array = [
[
1,
2,
3,
]
]
```

will be changed to:
Expand All @@ -137,6 +144,12 @@ foo([
2,
3,
])

nested_array = [[
1,
2,
3,
]]
```

This also applies to list and dictionary unpacking:
Expand Down
54 changes: 24 additions & 30 deletions src/black/handle_ipynb_magics.py
Expand Up @@ -17,36 +17,30 @@
from black.output import out
from black.report import NothingChanged

TRANSFORMED_MAGICS = frozenset(
(
"get_ipython().run_cell_magic",
"get_ipython().system",
"get_ipython().getoutput",
"get_ipython().run_line_magic",
)
)
TOKENS_TO_IGNORE = frozenset(
(
"ENDMARKER",
"NL",
"NEWLINE",
"COMMENT",
"DEDENT",
"UNIMPORTANT_WS",
"ESCAPED_NL",
)
)
PYTHON_CELL_MAGICS = frozenset(
(
"capture",
"prun",
"pypy",
"python",
"python3",
"time",
"timeit",
)
)
TRANSFORMED_MAGICS = frozenset((
"get_ipython().run_cell_magic",
"get_ipython().system",
"get_ipython().getoutput",
"get_ipython().run_line_magic",
))
TOKENS_TO_IGNORE = frozenset((
"ENDMARKER",
"NL",
"NEWLINE",
"COMMENT",
"DEDENT",
"UNIMPORTANT_WS",
"ESCAPED_NL",
))
PYTHON_CELL_MAGICS = frozenset((
"capture",
"prun",
"pypy",
"python",
"python3",
"time",
"timeit",
))
TOKEN_HEX = secrets.token_hex


Expand Down
15 changes: 9 additions & 6 deletions src/black/linegen.py
Expand Up @@ -817,18 +817,21 @@ def _first_right_hand_split(
body_leaves.reverse()
head_leaves.reverse()

if Preview.hug_parens_with_braces_and_square_brackets in line.mode:
if (
Preview.hug_parens_with_braces_and_square_brackets in line.mode
and tail_leaves[0].value
and tail_leaves[0].opening_bracket is head_leaves[-1]
):
is_unpacking = 1 if body_leaves[0].type in [token.STAR, token.DOUBLESTAR] else 0
if (
tail_leaves[0].type == token.RPAR
and tail_leaves[0].value
and tail_leaves[0].opening_bracket is head_leaves[-1]
and body_leaves[-1].type in [token.RBRACE, token.RSQB]
while (
len(body_leaves) >= 2 + is_unpacking
and body_leaves[-1].type in CLOSING_BRACKETS
and body_leaves[-1].opening_bracket is body_leaves[is_unpacking]
):
head_leaves = head_leaves + body_leaves[: 1 + is_unpacking]
tail_leaves = body_leaves[-1:] + tail_leaves
body_leaves = body_leaves[1 + is_unpacking : -1]
is_unpacking = 0

head = bracket_split_build_line(
head_leaves, line, opening_bracket, component=_BracketSplitComponent.head
Expand Down
6 changes: 3 additions & 3 deletions src/black/lines.py
Expand Up @@ -200,9 +200,9 @@ def is_triple_quoted_string(self) -> bool:
value = self.leaves[0].value
if value.startswith(('"""', "'''")):
return True
if Preview.accept_raw_docstrings in self.mode and value.startswith(
("r'''", 'r"""', "R'''", 'R"""')
yilei marked this conversation as resolved.
Show resolved Hide resolved
):
if Preview.accept_raw_docstrings in self.mode and value.startswith((
"r'''", 'r"""', "R'''", 'R"""'
)):
return True
return False

Expand Down
6 changes: 3 additions & 3 deletions src/blackd/middlewares.py
Expand Up @@ -36,9 +36,9 @@ async def impl(request: Request, handler: Handler) -> StreamResponse:
resp.headers["Access-Control-Expose-Headers"] = "*"
if is_options:
resp.headers["Access-Control-Allow-Headers"] = ", ".join(allow_headers)
resp.headers["Access-Control-Allow-Methods"] = ", ".join(
("OPTIONS", "POST")
)
resp.headers["Access-Control-Allow-Methods"] = ", ".join((
"OPTIONS", "POST"
))

return resp

Expand Down
6 changes: 3 additions & 3 deletions src/blib2to3/pgen2/tokenize.py
Expand Up @@ -314,9 +314,9 @@ def _get_normal_name(orig_enc: str) -> str:
enc = orig_enc[:12].lower().replace("_", "-")
if enc == "utf-8" or enc.startswith("utf-8-"):
return "utf-8"
if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or enc.startswith(
("latin-1-", "iso-8859-1-", "iso-latin-1-")
):
if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or enc.startswith((
"latin-1-", "iso-8859-1-", "iso-latin-1-"
)):
return "iso-8859-1"
return orig_enc

Expand Down
Expand Up @@ -128,6 +128,9 @@ def foo_square_brackets(request):
func({"short line"})
func({"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"})
func({{"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"}})
func(("long line", "long long line", "long long long line", "long long long long line", "long long long long long line"))
func((("long line", "long long line", "long long long line", "long long long long line", "long long long long long line")))
func([["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]])

foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
Expand All @@ -137,6 +140,10 @@ def foo_square_brackets(request):
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {x}, "a string", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)

nested_mapping = {"key": [{"a very long key 1": "with a very long value", "a very long key 2": "with a very long value"}]}
nested_array = [[["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]]]
explicit_exploding = [[["short", "line",],],]

foo(*["long long long long long line", "long long long long long line", "long long long long long line"])

foo(*[str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)])
Expand Down Expand Up @@ -285,15 +292,34 @@ def foo_square_brackets(request):
"long long long long line",
"long long long long long line",
})
func({
{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}
})
func({{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}})
func((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
))
func(((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
)))
func([[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]])

foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
Expand All @@ -303,6 +329,28 @@ def foo_square_brackets(request):
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {x}, "a string", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)

nested_mapping = {
"key": [{
"a very long key 1": "with a very long value",
"a very long key 2": "with a very long value",
}]
}
nested_array = [[[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]]]
explicit_exploding = [
[
[
"short",
"line",
],
],
]

foo(*[
"long long long long long line",
"long long long long long line",
Expand Down
15 changes: 7 additions & 8 deletions tests/data/cases/preview_long_strings__regression.py
Expand Up @@ -611,14 +611,13 @@ def foo():

class A:
def foo():
XXXXXXXXXXXX.append(
(
"xxx_xxxxxxxxxx(xxxxx={}, xxxx={}, xxxxx, xxxx_xxxx_xxxxxxxxxx={})"
.format(xxxxx, xxxx, xxxx_xxxx_xxxxxxxxxx),
my_var,
my_other_var,
)
)
XXXXXXXXXXXX.append((
"xxx_xxxxxxxxxx(xxxxx={}, xxxx={}, xxxxx, xxxx_xxxx_xxxxxxxxxx={})".format(
xxxxx, xxxx, xxxx_xxxx_xxxxxxxxxx
),
my_var,
my_other_var,
))


class A:
Expand Down