Skip to content

Fix bug with standalone comments in lambda default arguments #4658

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Fix crash when a tuple appears in the `as` clause of a `with` statement
(#4634)
- Fix crash when tuple is used as a context manager inside a `with` statement (#4646)
- Fix crash when standalone comment is within parentheses in lambda default arguments (#4640)

### Preview style

Expand Down
18 changes: 15 additions & 3 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,21 @@ def has_magic_trailing_comma(self, closing: Leaf) -> bool:
if self.is_import:
return True

if closing.opening_bracket is not None and not is_one_sequence_between(
closing.opening_bracket, closing, self.leaves
):
# Check if there are any standalone comments in the line
# If so, we need to split the line, so return True
for leaf in self.leaves:
if leaf.type == STANDALONE_COMMENT:
return True

# Only try is_one_sequence_between if there are no standalone comments
try:
if closing.opening_bracket is not None and not is_one_sequence_between(
closing.opening_bracket, closing, self.leaves
):
return True
except LookupError:
# If we can't find the opening bracket, assume we need to split
# This can happen with standalone comments in lambda default arguments
return True

return False
Expand Down
83 changes: 83 additions & 0 deletions tests/data/cases/lambda_default_with_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Test case for issue #4640
# Standalone comment within parentheses in lambda default arguments

# Basic case: Comment at the beginning of lambda default arguments
help(
lambda x=(
# comment
"bar",
): False,
)

# Basic case: Comment in the middle of lambda default arguments
help(
lambda x=("bar",
# comment
): False
)

# Basic case: Comment with long arguments
help(
lambda x=("extremely lengthy argument 1 extremely lengthy argument 1",
# comment
"extremely lengthy argument 2 extremely lengthy argument 2",
): False,
)

# Edge case: Multiple comments in different positions
help(
lambda x=(
# comment 1
"bar",
# comment 2
"baz",
# comment 3
): False,
)

# Edge case: Nested lambdas with comments
help(
lambda x=(
# comment before nested lambda
lambda y: (
# comment inside nested lambda
y
+ 1
),
): False,
)

# Edge case: Comments with special characters
help(
lambda x=(
# comment with * and ** and () and [] and {}
"bar",
): False,
)

# Edge case: Empty tuple with comment
help(
lambda x=(
# comment in empty tuple
): False,
)

# Edge case: Comment at the beginning and end
help(
lambda x=(
# comment at beginning
"bar", "baz",
# comment at end
): False,
)

# Edge case: Multiple arguments with multiple comments
f = lambda x=(
# comment 1
"foo",
# comment 2
"bar",
# comment 3
"baz",
# comment 4
): x
89 changes: 89 additions & 0 deletions tests/data/cases/lambda_default_with_comment_transformation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Test case for issue #4640
# Transformation test for standalone comments within parentheses in lambda default arguments

# Unformatted input with various cases of standalone comments in lambda default arguments
help(
lambda x=(
# comment at beginning
"bar",
): False,
)

help(
lambda x=("bar",
# comment in middle
): False
)

help(
lambda x=("extremely lengthy argument 1 extremely lengthy argument 1",
# comment with long arguments
"extremely lengthy argument 2 extremely lengthy argument 2",
): False,
)

help(
lambda x=(
# comment 1
"bar",
# comment 2
"baz",
# comment 3
): False
)

f = lambda x=(
# comment 1
"foo",
# comment 2
"bar",
# comment 3
"baz",
# comment 4
): x

# output

# Test case for issue #4640
# Transformation test for standalone comments within parentheses in lambda default arguments

# Unformatted input with various cases of standalone comments in lambda default arguments
help(
lambda x=(
# comment at beginning
"bar",
): False,
)

help(
lambda x=("bar",
# comment in middle
): False
)

help(
lambda x=("extremely lengthy argument 1 extremely lengthy argument 1",
# comment with long arguments
"extremely lengthy argument 2 extremely lengthy argument 2",
): False,
)

help(
lambda x=(
# comment 1
"bar",
# comment 2
"baz",
# comment 3
): False
)

f = lambda x=(
# comment 1
"foo",
# comment 2
"bar",
# comment 3
"baz",
# comment 4
): x
Loading