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

Draft for Black 2023 stable style #3418

Merged
merged 39 commits into from Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f58f446
Clean up comments.py preview mode
JelleZijlstra Dec 10, 2022
2caca04
Clean up preview in __init__.py
JelleZijlstra Dec 10, 2022
9a68676
clean up normalize_invisible_parens preview
JelleZijlstra Dec 10, 2022
00cd371
more in linegen.py
JelleZijlstra Dec 10, 2022
b6cd780
annotation_parens
JelleZijlstra Dec 10, 2022
714af8b
empty_lines_before_class_or_def_with_leading_comments
JelleZijlstra Dec 10, 2022
fe271ed
handle_trailing_commas_in_head
JelleZijlstra Dec 10, 2022
9237082
normalize_docstring_quotes_and_prefixes_properly (unused)
JelleZijlstra Dec 10, 2022
fc2acb2
one_element_subscript
JelleZijlstra Dec 10, 2022
ecfa7b6
remove_block_trailing_newline
JelleZijlstra Dec 10, 2022
11b0f1e
remove_redundant_parens
JelleZijlstra Dec 10, 2022
0152d51
skip_magic_trailing_comma_in_subscript
JelleZijlstra Dec 10, 2022
4329c9f
unused import
JelleZijlstra Dec 10, 2022
e44aa09
fix path in test
JelleZijlstra Dec 10, 2022
c4c7072
expression_skip_magic_trailing_comma.diff
JelleZijlstra Dec 10, 2022
7fbdc95
Revert "annotation_parens"
JelleZijlstra Dec 10, 2022
45fd512
Merge branch 'main' into black23
JelleZijlstra Dec 17, 2022
42624f4
fixup after merge
JelleZijlstra Dec 17, 2022
bce883b
enable long_docstring_quotes_on_newline
JelleZijlstra Dec 17, 2022
d0b42a7
put prefer_splitting_right_hand_side_of_assignments back
JelleZijlstra Dec 17, 2022
23840a5
Merge branch 'main' into black23
JelleZijlstra Dec 18, 2022
c5fc62d
add annotation_parens
JelleZijlstra Dec 18, 2022
0528f07
move some tests out of preview
JelleZijlstra Dec 18, 2022
4842546
fix a test
JelleZijlstra Dec 18, 2022
4acf335
Revert "enable long_docstring_quotes_on_newline"
JelleZijlstra Dec 18, 2022
d48f8ed
fix line-length 1 bug
JelleZijlstra Dec 18, 2022
387f3fb
fix tests
JelleZijlstra Dec 18, 2022
c9280bf
move more tests to non-preview
JelleZijlstra Dec 18, 2022
88957e5
changelog entry
JelleZijlstra Dec 18, 2022
a5bc399
delete preview_39/310
JelleZijlstra Dec 18, 2022
8aa39b6
fix changelog
JelleZijlstra Dec 18, 2022
5f95986
Merge remote-tracking branch 'upstream/main' into black23
JelleZijlstra Dec 23, 2022
70e2142
Merge branch 'main' into black23
JelleZijlstra Dec 29, 2022
7b3fd6a
Merge branch 'main' into black23
JelleZijlstra Jan 18, 2023
2b0aea9
Merge branch 'main' into black23
JelleZijlstra Jan 31, 2023
1be7214
fix up merge
JelleZijlstra Jan 31, 2023
2c0b333
now we do remove them
JelleZijlstra Jan 31, 2023
0da89c4
Merge remote-tracking branch 'upstream/main' into black23
JelleZijlstra Jan 31, 2023
1c8d157
Update style docs
ichard26 Jan 31, 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
29 changes: 29 additions & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,35 @@

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

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

### Preview style
Expand Down
40 changes: 39 additions & 1 deletion docs/the_black_code_style/current_style.md
Expand Up @@ -194,7 +194,45 @@ that in-function vertical whitespace should only be used sparingly.
_Black_ will allow single empty lines inside functions, and single and double empty
lines on module level left by the original editors, except when they're within
parenthesized expressions. Since such expressions are always reformatted to fit minimal
space, this whitespace is lost.
space, this whitespace is lost. The other exception is that it will remove any empty
lines immediately following a statement that introduces a new indentation level.

```python
# in:

def foo():

print("All the newlines above me should be deleted!")


if condition:

print("No newline above me!")

print("There is a newline above me, and that's OK!")


class Point:

x: int
y: int

# out:

def foo():
print("All the newlines above me should be deleted!")


if condition:
print("No newline above me!")

print("There is a newline above me, and that's OK!")


class Point:
x: int
y: int
```

It will also insert proper spacing before and after function definitions. It's one line
before and after inner functions and two lines before and after module-level functions
Expand Down
90 changes: 0 additions & 90 deletions docs/the_black_code_style/future_style.md
Expand Up @@ -62,93 +62,3 @@ plain strings. User-made splits are respected when they do not exceed the line l
limit. Line continuation backslashes are converted into parenthesized strings.
Unnecessary parentheses are stripped. The stability and status of this feature is
tracked in [this issue](https://github.com/psf/black/issues/2188).

### Improved empty line management

1. _Black_ will remove newlines in the beginning of new code blocks, i.e. when the
indentation level is increased. For example:

```python
def my_func():

print("The line above me will be deleted!")
```

will be changed to:

```python
def my_func():
print("The line above me will be deleted!")
```

This new feature will be applied to **all code blocks**: `def`, `class`, `if`,
`for`, `while`, `with`, `case` and `match`.

2. _Black_ will enforce empty lines before classes and functions with leading comments.
For example:

```python
some_var = 1
# Leading sticky comment
def my_func():
...
```

will be changed to:

```python
some_var = 1


# Leading sticky comment
def my_func():
...
```

### Improved parentheses management

_Black_ will format parentheses around return annotations similarly to other sets of
parentheses. For example:

```python
def foo() -> (int):
...

def foo() -> looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong:
...
```

will be changed to:

```python
def foo() -> int:
...


def foo() -> (
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
):
...
```

And, extra parentheses in `await` expressions and `with` statements are removed. For
example:

```python
with ((open("bla.txt")) as f, open("x")):
...

async def main():
await (asyncio.sleep(1))
```

will be changed to:

```python
with open("bla.txt") as f, open("x"):
...


async def main():
await asyncio.sleep(1)
```
9 changes: 3 additions & 6 deletions src/black/__init__.py
Expand Up @@ -925,9 +925,6 @@ def format_file_contents(src_contents: str, *, fast: bool, mode: Mode) -> FileCo
valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
`mode` is passed to :func:`format_str`.
"""
if not mode.preview and not src_contents.strip():
raise NothingChanged

if mode.is_ipynb:
dst_contents = format_ipynb_string(src_contents, fast=fast, mode=mode)
else:
Expand Down Expand Up @@ -1022,7 +1019,7 @@ def format_ipynb_string(src_contents: str, *, fast: bool, mode: Mode) -> FileCon
Operate cell-by-cell, only on code cells, only for Python notebooks.
If the ``.ipynb`` originally had a trailing newline, it'll be preserved.
"""
if mode.preview and not src_contents:
if not src_contents:
raise NothingChanged

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