Skip to content

Commit c4bd2e3

Browse files
Draft for Black 2023 stable style (#3418)
1 parent 226cbf0 commit c4bd2e3

36 files changed

+207
-274
lines changed

CHANGES.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@
1010

1111
<!-- Changes that affect Black's stable style -->
1212

13+
- Introduce the 2023 stable style, which incorporates most aspects of last year's
14+
preview style (#3418). Specific changes:
15+
- Enforce empty lines before classes and functions with sticky leading comments
16+
(#3302) (22.12.0)
17+
- Reformat empty and whitespace-only files as either an empty file (if no newline is
18+
present) or as a single newline character (if a newline is present) (#3348)
19+
(22.12.0)
20+
- Implicitly concatenated strings used as function args are now wrapped inside
21+
parentheses (#3307) (22.12.0)
22+
- Correctly handle trailing commas that are inside a line's leading non-nested parens
23+
(#3370) (22.12.0)
24+
- `--skip-string-normalization` / `-S` now prevents docstring prefixes from being
25+
normalized as expected (#3168) (since 22.8.0)
26+
- When using `--skip-magic-trailing-comma` or `-C`, trailing commas are stripped from
27+
subscript expressions with more than 1 element (#3209) (22.8.0)
28+
- Implicitly concatenated strings inside a list, set, or tuple are now wrapped inside
29+
parentheses (#3162) (22.8.0)
30+
- Fix a string merging/split issue when a comment is present in the middle of
31+
implicitly concatenated strings on its own line (#3227) (22.8.0)
32+
- Docstring quotes are no longer moved if it would violate the line length limit
33+
(#3044, #3430) (22.6.0)
34+
- Parentheses around return annotations are now managed (#2990) (22.6.0)
35+
- Remove unnecessary parentheses around awaited objects (#2991) (22.6.0)
36+
- Remove unnecessary parentheses in `with` statements (#2926) (22.6.0)
37+
- Remove trailing newlines after code block open (#3035) (22.6.0)
38+
- Code cell separators `#%%` are now standardised to `# %%` (#2919) (22.3.0)
39+
- Remove unnecessary parentheses from `except` statements (#2939) (22.3.0)
40+
- Remove unnecessary parentheses from tuple unpacking in `for` loops (#2945) (22.3.0)
41+
- Avoid magic-trailing-comma in single-element subscripts (#2942) (22.3.0)
1342
- Fix a crash when a colon line is marked between `# fmt: off` and `# fmt: on` (#3439)
1443

1544
### Preview style

docs/the_black_code_style/current_style.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,45 @@ that in-function vertical whitespace should only be used sparingly.
194194
_Black_ will allow single empty lines inside functions, and single and double empty
195195
lines on module level left by the original editors, except when they're within
196196
parenthesized expressions. Since such expressions are always reformatted to fit minimal
197-
space, this whitespace is lost.
197+
space, this whitespace is lost. The other exception is that it will remove any empty
198+
lines immediately following a statement that introduces a new indentation level.
199+
200+
```python
201+
# in:
202+
203+
def foo():
204+
205+
print("All the newlines above me should be deleted!")
206+
207+
208+
if condition:
209+
210+
print("No newline above me!")
211+
212+
print("There is a newline above me, and that's OK!")
213+
214+
215+
class Point:
216+
217+
x: int
218+
y: int
219+
220+
# out:
221+
222+
def foo():
223+
print("All the newlines above me should be deleted!")
224+
225+
226+
if condition:
227+
print("No newline above me!")
228+
229+
print("There is a newline above me, and that's OK!")
230+
231+
232+
class Point:
233+
x: int
234+
y: int
235+
```
198236

199237
It will also insert proper spacing before and after function definitions. It's one line
200238
before and after inner functions and two lines before and after module-level functions

docs/the_black_code_style/future_style.md

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -62,93 +62,3 @@ plain strings. User-made splits are respected when they do not exceed the line l
6262
limit. Line continuation backslashes are converted into parenthesized strings.
6363
Unnecessary parentheses are stripped. The stability and status of this feature is
6464
tracked in [this issue](https://github.com/psf/black/issues/2188).
65-
66-
### Improved empty line management
67-
68-
1. _Black_ will remove newlines in the beginning of new code blocks, i.e. when the
69-
indentation level is increased. For example:
70-
71-
```python
72-
def my_func():
73-
74-
print("The line above me will be deleted!")
75-
```
76-
77-
will be changed to:
78-
79-
```python
80-
def my_func():
81-
print("The line above me will be deleted!")
82-
```
83-
84-
This new feature will be applied to **all code blocks**: `def`, `class`, `if`,
85-
`for`, `while`, `with`, `case` and `match`.
86-
87-
2. _Black_ will enforce empty lines before classes and functions with leading comments.
88-
For example:
89-
90-
```python
91-
some_var = 1
92-
# Leading sticky comment
93-
def my_func():
94-
...
95-
```
96-
97-
will be changed to:
98-
99-
```python
100-
some_var = 1
101-
102-
103-
# Leading sticky comment
104-
def my_func():
105-
...
106-
```
107-
108-
### Improved parentheses management
109-
110-
_Black_ will format parentheses around return annotations similarly to other sets of
111-
parentheses. For example:
112-
113-
```python
114-
def foo() -> (int):
115-
...
116-
117-
def foo() -> looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong:
118-
...
119-
```
120-
121-
will be changed to:
122-
123-
```python
124-
def foo() -> int:
125-
...
126-
127-
128-
def foo() -> (
129-
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
130-
):
131-
...
132-
```
133-
134-
And, extra parentheses in `await` expressions and `with` statements are removed. For
135-
example:
136-
137-
```python
138-
with ((open("bla.txt")) as f, open("x")):
139-
...
140-
141-
async def main():
142-
await (asyncio.sleep(1))
143-
```
144-
145-
will be changed to:
146-
147-
```python
148-
with open("bla.txt") as f, open("x"):
149-
...
150-
151-
152-
async def main():
153-
await asyncio.sleep(1)
154-
```

src/black/__init__.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,6 @@ def format_file_contents(src_contents: str, *, fast: bool, mode: Mode) -> FileCo
925925
valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
926926
`mode` is passed to :func:`format_str`.
927927
"""
928-
if not mode.preview and not src_contents.strip():
929-
raise NothingChanged
930-
931928
if mode.is_ipynb:
932929
dst_contents = format_ipynb_string(src_contents, fast=fast, mode=mode)
933930
else:
@@ -1022,7 +1019,7 @@ def format_ipynb_string(src_contents: str, *, fast: bool, mode: Mode) -> FileCon
10221019
Operate cell-by-cell, only on code cells, only for Python notebooks.
10231020
If the ``.ipynb`` originally had a trailing newline, it'll be preserved.
10241021
"""
1025-
if mode.preview and not src_contents:
1022+
if not src_contents:
10261023
raise NothingChanged
10271024

10281025
trailing_newline = src_contents[-1] == "\n"
@@ -1101,7 +1098,7 @@ def _format_str_once(src_contents: str, *, mode: Mode) -> str:
11011098
for feature in {Feature.PARENTHESIZED_CONTEXT_MANAGERS}
11021099
if supports_feature(versions, feature)
11031100
}
1104-
normalize_fmt_off(src_node, preview=mode.preview)
1101+
normalize_fmt_off(src_node)
11051102
lines = LineGenerator(mode=mode, features=context_manager_features)
11061103
elt = EmptyLineTracker(mode=mode)
11071104
split_line_features = {
@@ -1122,7 +1119,7 @@ def _format_str_once(src_contents: str, *, mode: Mode) -> str:
11221119
dst_contents = []
11231120
for block in dst_blocks:
11241121
dst_contents.extend(block.all_lines())
1125-
if mode.preview and not dst_contents:
1122+
if not dst_contents:
11261123
# Use decode_bytes to retrieve the correct source newline (CRLF or LF),
11271124
# and check if normalized_content has more than one line
11281125
normalized_content, _, newline = decode_bytes(src_contents.encode("utf-8"))

0 commit comments

Comments
 (0)