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 all 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

<!-- Changes that affect Black's preview style -->

- Additional cases of immediately nested tuples, lists, and dictionaries are now
indented less (#4012)

### Configuration

<!-- Changes to how Black can be configured -->
Expand Down
36 changes: 16 additions & 20 deletions docs/conf.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions src/black/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ def filtered_cached(self, sources: Iterable[Path]) -> Tuple[Set[Path], Set[Path]

def write(self, sources: Iterable[Path]) -> None:
"""Update the cache file data and write a new cache file."""
self.file_data.update(**{
str(src.resolve()): Cache.get_file_data(src) for src in sources
})
self.file_data.update(
**{str(src.resolve()): Cache.get_file_data(src) for src in sources}
)
try:
CACHE_DIR.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile(
Expand Down
54 changes: 24 additions & 30 deletions src/black/handle_ipynb_magics.py
Original file line number Diff line number Diff line change
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
69 changes: 55 additions & 14 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,25 +817,66 @@ def _first_right_hand_split(
body_leaves.reverse()
head_leaves.reverse()

if Preview.hug_parens_with_braces_and_square_brackets in line.mode:
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]
and body_leaves[-1].opening_bracket is body_leaves[is_unpacking]
body: Optional[Line] = None
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]
):
inner_body_leaves = list(body_leaves)
hugged_opening_leaves: List[Leaf] = []
hugged_closing_leaves: List[Leaf] = []
is_unpacking = body_leaves[0].type in [token.STAR, token.DOUBLESTAR]
unpacking_offset: int = 1 if is_unpacking else 0
while (
len(inner_body_leaves) >= 2 + unpacking_offset
and inner_body_leaves[-1].type in CLOSING_BRACKETS
and inner_body_leaves[-1].opening_bracket
is inner_body_leaves[unpacking_offset]
):
head_leaves = head_leaves + body_leaves[: 1 + is_unpacking]
tail_leaves = body_leaves[-1:] + tail_leaves
body_leaves = body_leaves[1 + is_unpacking : -1]
if unpacking_offset:
hugged_opening_leaves.append(inner_body_leaves.pop(0))
unpacking_offset = 0
hugged_opening_leaves.append(inner_body_leaves.pop(0))
hugged_closing_leaves.insert(0, inner_body_leaves.pop())

if hugged_opening_leaves and inner_body_leaves:
inner_body = bracket_split_build_line(
inner_body_leaves,
line,
hugged_opening_leaves[-1],
component=_BracketSplitComponent.body,
)
if (
line.mode.magic_trailing_comma
and inner_body_leaves[-1].type == token.COMMA
):
should_hug = True
else:
line_length = line.mode.line_length - sum(
len(str(leaf))
for leaf in hugged_opening_leaves + hugged_closing_leaves
)
if is_line_short_enough(
inner_body, mode=replace(line.mode, line_length=line_length)
):
# Do not hug if it fits on a single line.
should_hug = False
else:
should_hug = True
if should_hug:
body_leaves = inner_body_leaves
head_leaves.extend(hugged_opening_leaves)
tail_leaves = hugged_closing_leaves + tail_leaves
body = inner_body # No need to re-calculate the body again later.

head = bracket_split_build_line(
head_leaves, line, opening_bracket, component=_BracketSplitComponent.head
)
body = bracket_split_build_line(
body_leaves, line, opening_bracket, component=_BracketSplitComponent.body
)
if body is None:
body = bracket_split_build_line(
body_leaves, line, opening_bracket, component=_BracketSplitComponent.body
)
tail = bracket_split_build_line(
tail_leaves, line, opening_bracket, component=_BracketSplitComponent.tail
)
Expand Down
Loading