Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from black.brackets import (
COMMA_PRIORITY,
DOT_PRIORITY,
LOGIC_PRIORITY,
STRING_PRIORITY,
get_leaves_inside_matching_brackets,
max_delimiter_priority_in_atom,
Expand Down Expand Up @@ -1384,6 +1385,21 @@ def delimiter_split(
):
raise CannotSplit("Splitting a single attribute from its owner looks wrong")

# When a standalone comment is the first token on the line and precedes an
# arithmetic/comparator expression, let standalone_comment_split handle it so
# the expression can stay on one line if it fits (issue #3713). Only applies
# when the comment leads the line (not trailing) and the delimiter has lower
# priority than logical operators to avoid changing `or`/`and` behavior.
if (
line.leaves
and line.leaves[0].type == STANDALONE_COMMENT
and delimiter_priority < LOGIC_PRIORITY
):
raise CannotSplit(
"Standalone comment leads line before arithmetic delimiter; deferring to"
" standalone_comment_split"
)

current_line = Line(
mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
)
Expand Down
42 changes: 42 additions & 0 deletions tests/data/cases/comment_before_binary_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
my_func(
arg1=1,
arg2=[
func_call(
# Comment.
[MyClass(arg1, arg2)] * 10000
),
],
)

result = func_call(
# Comment.
value * 10000
)

result = func_call(
# Comment.
value + other_value
)


# output

my_func(
arg1=1,
arg2=[
func_call(
# Comment.
[MyClass(arg1, arg2)] * 10000
),
],
)

result = func_call(
# Comment.
value * 10000
)

result = func_call(
# Comment.
value + other_value
)
4 changes: 1 addition & 3 deletions tests/data/cases/function_trailing_comma.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,7 @@ def foo() -> (

def foo() -> (
# comment inside parenthesised new union return type
int
| str
| bytes
int | str | bytes
): ...


Expand Down
Loading