Skip to content

Commit

Permalink
Merge branch 'main' into bytedoc
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Oct 31, 2023
2 parents 8163f2d + e501103 commit 48aa44a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
### Stable style

- Fix crash on formatting bytes strings that look like docstrings (#4003)
- Fix crash when whitespace followed a backslash before newline in a docstring
(#4008)

### Preview style

- Multiline dictionaries and lists that are the sole argument to a function are now
indented less (#3964)
- Multiline list and dict unpacking as the sole argument to a function is now also
indented less (#3992)

### Configuration

Expand Down
20 changes: 20 additions & 0 deletions docs/the_black_code_style/future_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ foo([
])
```

This also applies to list and dictionary unpacking:

```python
foo(
*[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
```

will become:

```python
foo(*[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
])
```

You can use a magic trailing comma to avoid this compacting behavior; by default,
_Black_ will not reformat the following code:

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
10 changes: 6 additions & 4 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Generating lines of code.
"""

import re
import sys
from dataclasses import replace
from enum import Enum, auto
Expand Down Expand Up @@ -420,7 +421,7 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
if Preview.hex_codes_in_unicode_sequences in self.mode:
normalize_unicode_escape_sequences(leaf)

if is_docstring(leaf) and "\\\n" not in leaf.value:
if is_docstring(leaf) and not re.search(r"\\\s*\n", leaf.value):
# We're ignoring docstrings with backslash newline escapes because changing
# indentation of those changes the AST representation of the code.
if self.mode.string_normalization:
Expand Down Expand Up @@ -817,16 +818,17 @@ def _first_right_hand_split(
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[0]
and body_leaves[-1].opening_bracket is body_leaves[is_unpacking]
):
head_leaves = head_leaves + body_leaves[:1]
head_leaves = head_leaves + body_leaves[: 1 + is_unpacking]
tail_leaves = body_leaves[-1:] + tail_leaves
body_leaves = body_leaves[1:-1]
body_leaves = body_leaves[1 + is_unpacking : -1]

head = bracket_split_build_line(
head_leaves, line, opening_bracket, component=_BracketSplitComponent.head
Expand Down
13 changes: 13 additions & 0 deletions tests/data/cases/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
'''


def foo():
"""
Docstring with a backslash followed by a space\
and then another line
"""

# output

class MyClass:
Expand Down Expand Up @@ -442,3 +448,10 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
<text here, since without another non-empty line black is stable>
"""


def foo():
"""
Docstring with a backslash followed by a space\
and then another line
"""
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ 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]
)

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)])

foo(
**{
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": 1,
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": 2,
"ccccccccccccccccccccccccccccccccc": 3,
**other,
}
)

foo(**{x: y for x, y in enumerate(["long long long long line","long long long long line"])})

# output
def foo_brackets(request):
return JsonResponse({
Expand Down Expand Up @@ -287,3 +302,24 @@ def foo_square_brackets(request):
baaaaaaaaaaaaar(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {x}, "a string", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)

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)
])

foo(**{
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": 1,
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": 2,
"ccccccccccccccccccccccccccccccccc": 3,
**other,
})

foo(**{
x: y for x, y in enumerate(["long long long long line", "long long long long line"])
})

0 comments on commit 48aa44a

Please sign in to comment.