Skip to content

Commit 8e1dfd3

Browse files
authored
Merge branch 'main' into gh-4220
2 parents 54a89f5 + 0f18001 commit 8e1dfd3

File tree

3 files changed

+34
-42
lines changed

3 files changed

+34
-42
lines changed

docs/conf.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515

1616
import os
1717
import string
18+
from importlib.metadata import version
1819
from pathlib import Path
1920

20-
from pkg_resources import get_distribution
21-
2221
CURRENT_DIR = Path(__file__).parent
2322

2423

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

4443
# Autopopulate version
4544
# The version, including alpha/beta/rc tags, but not commit hash and datestamps
46-
release = get_distribution("black").version.split("+")[0]
45+
release = version("black").split("+")[0]
4746
# The short X.Y version.
4847
version = release
4948
for sp in "abcfr":

docs/the_black_code_style/current_style.md

+15-24
Original file line numberDiff line numberDiff line change
@@ -166,44 +166,35 @@ that in-function vertical whitespace should only be used sparingly.
166166
_Black_ will allow single empty lines inside functions, and single and double empty
167167
lines on module level left by the original editors, except when they're within
168168
parenthesized expressions. Since such expressions are always reformatted to fit minimal
169-
space, this whitespace is lost. The other exception is that it will remove any empty
170-
lines immediately following a statement that introduces a new indentation level.
169+
space, this whitespace is lost.
171170

172171
```python
173172
# in:
174173

175-
def foo():
174+
def function(
175+
some_argument: int,
176176

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

179180

180-
if condition:
181181

182-
print("No newline above me!")
183-
184-
print("There is a newline above me, and that's OK!")
185-
186-
187-
class Point:
188-
189-
x: int
190-
y: int
182+
print("One empty line above me will be kept!")
191183

184+
def this_is_okay_too():
185+
print("No empty line here")
192186
# out:
193187

194-
def foo():
195-
print("All the newlines above me should be deleted!")
196-
197-
198-
if condition:
199-
print("No newline above me!")
188+
def function(
189+
some_argument: int,
190+
other_argument: int = 5,
191+
) -> EmptyLineInParenWillBeDeleted:
200192

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

203195

204-
class Point:
205-
x: int
206-
y: int
196+
def this_is_okay_too():
197+
print("No empty line here")
207198
```
208199

209200
It will also insert proper spacing before and after function definitions. It's one line

src/black/linegen.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,14 @@ def _get_last_non_comment_leaf(line: Line) -> Optional[int]:
11361136
return None
11371137

11381138

1139+
def _can_add_trailing_comma(leaf: Leaf, features: Collection[Feature]) -> bool:
1140+
if is_vararg(leaf, within={syms.typedargslist}):
1141+
return Feature.TRAILING_COMMA_IN_DEF in features
1142+
if is_vararg(leaf, within={syms.arglist, syms.argument}):
1143+
return Feature.TRAILING_COMMA_IN_CALL in features
1144+
return True
1145+
1146+
11391147
def _safe_add_trailing_comma(safe: bool, delimiter_priority: int, line: Line) -> Line:
11401148
if (
11411149
safe
@@ -1160,20 +1168,21 @@ def delimiter_split(
11601168
If the appropriate Features are given, the split will add trailing commas
11611169
also in function signatures and calls that contain `*` and `**`.
11621170
"""
1163-
try:
1164-
last_leaf = line.leaves[-1]
1165-
except IndexError:
1171+
if len(line.leaves) == 0:
11661172
raise CannotSplit("Line empty") from None
1173+
last_leaf = line.leaves[-1]
11671174

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

1174-
if delimiter_priority == DOT_PRIORITY:
1175-
if bt.delimiter_count_with_priority(delimiter_priority) == 1:
1176-
raise CannotSplit("Splitting a single attribute from its owner looks wrong")
1181+
if (
1182+
delimiter_priority == DOT_PRIORITY
1183+
and bt.delimiter_count_with_priority(delimiter_priority) == 1
1184+
):
1185+
raise CannotSplit("Splitting a single attribute from its owner looks wrong")
11771186

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

12141223
lowest_depth = min(lowest_depth, leaf.bracket_depth)
1215-
if leaf.bracket_depth == lowest_depth:
1216-
if is_vararg(leaf, within={syms.typedargslist}):
1217-
trailing_comma_safe = (
1218-
trailing_comma_safe and Feature.TRAILING_COMMA_IN_DEF in features
1219-
)
1220-
elif is_vararg(leaf, within={syms.arglist, syms.argument}):
1221-
trailing_comma_safe = (
1222-
trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features
1223-
)
1224+
if trailing_comma_safe and leaf.bracket_depth == lowest_depth:
1225+
trailing_comma_safe = _can_add_trailing_comma(leaf, features)
12241226

12251227
if last_leaf.type == STANDALONE_COMMENT and leaf_idx == last_non_comment_leaf:
12261228
current_line = _safe_add_trailing_comma(

0 commit comments

Comments
 (0)