Skip to content

Commit

Permalink
Merge branch 'main' into psfgh-4220
Browse files Browse the repository at this point in the history
  • Loading branch information
cobaltt7 authored Feb 28, 2024
2 parents 54a89f5 + 0f18001 commit 8e1dfd3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
5 changes: 2 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@

import os
import string
from importlib.metadata import version
from pathlib import Path

from pkg_resources import get_distribution

CURRENT_DIR = Path(__file__).parent


Expand All @@ -43,7 +42,7 @@ def make_pypi_svg(version: str) -> None:

# Autopopulate version
# The version, including alpha/beta/rc tags, but not commit hash and datestamps
release = get_distribution("black").version.split("+")[0]
release = version("black").split("+")[0]
# The short X.Y version.
version = release
for sp in "abcfr":
Expand Down
39 changes: 15 additions & 24 deletions docs/the_black_code_style/current_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,44 +166,35 @@ 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. The other exception is that it will remove any empty
lines immediately following a statement that introduces a new indentation level.
space, this whitespace is lost.

```python
# in:

def foo():
def function(
some_argument: int,

print("All the newlines above me should be deleted!")
other_argument: int = 5,
) -> EmptyLineInParenWillBeDeleted:


if condition:

print("No newline above me!")

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


class Point:

x: int
y: int
print("One empty line above me will be kept!")

def this_is_okay_too():
print("No empty line here")
# out:

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


if condition:
print("No newline above me!")
def function(
some_argument: int,
other_argument: int = 5,
) -> EmptyLineInParenWillBeDeleted:

print("There is a newline above me, and that's OK!")
print("One empty line above me will be kept!")


class Point:
x: int
y: int
def this_is_okay_too():
print("No empty line here")
```

It will also insert proper spacing before and after function definitions. It's one line
Expand Down
32 changes: 17 additions & 15 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,14 @@ def _get_last_non_comment_leaf(line: Line) -> Optional[int]:
return None


def _can_add_trailing_comma(leaf: Leaf, features: Collection[Feature]) -> bool:
if is_vararg(leaf, within={syms.typedargslist}):
return Feature.TRAILING_COMMA_IN_DEF in features
if is_vararg(leaf, within={syms.arglist, syms.argument}):
return Feature.TRAILING_COMMA_IN_CALL in features
return True


def _safe_add_trailing_comma(safe: bool, delimiter_priority: int, line: Line) -> Line:
if (
safe
Expand All @@ -1160,20 +1168,21 @@ def delimiter_split(
If the appropriate Features are given, the split will add trailing commas
also in function signatures and calls that contain `*` and `**`.
"""
try:
last_leaf = line.leaves[-1]
except IndexError:
if len(line.leaves) == 0:
raise CannotSplit("Line empty") from None
last_leaf = line.leaves[-1]

bt = line.bracket_tracker
try:
delimiter_priority = bt.max_delimiter_priority(exclude={id(last_leaf)})
except ValueError:
raise CannotSplit("No delimiters found") from None

if delimiter_priority == DOT_PRIORITY:
if bt.delimiter_count_with_priority(delimiter_priority) == 1:
raise CannotSplit("Splitting a single attribute from its owner looks wrong")
if (
delimiter_priority == DOT_PRIORITY
and bt.delimiter_count_with_priority(delimiter_priority) == 1
):
raise CannotSplit("Splitting a single attribute from its owner looks wrong")

current_line = Line(
mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
Expand Down Expand Up @@ -1212,15 +1221,8 @@ def append_comments(leaf: Leaf) -> Iterator[Line]:
yield from append_comments(leaf)

lowest_depth = min(lowest_depth, leaf.bracket_depth)
if leaf.bracket_depth == lowest_depth:
if is_vararg(leaf, within={syms.typedargslist}):
trailing_comma_safe = (
trailing_comma_safe and Feature.TRAILING_COMMA_IN_DEF in features
)
elif is_vararg(leaf, within={syms.arglist, syms.argument}):
trailing_comma_safe = (
trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features
)
if trailing_comma_safe and leaf.bracket_depth == lowest_depth:
trailing_comma_safe = _can_add_trailing_comma(leaf, features)

if last_leaf.type == STANDALONE_COMMENT and leaf_idx == last_non_comment_leaf:
current_line = _safe_add_trailing_comma(
Expand Down

0 comments on commit 8e1dfd3

Please sign in to comment.