Skip to content

Standalone comment within parentheses in lambda default arguments crashes black #4640

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
tjstum opened this issue Mar 31, 2025 · 4 comments · May be fixed by #4658
Open

Standalone comment within parentheses in lambda default arguments crashes black #4640

tjstum opened this issue Mar 31, 2025 · 4 comments · May be fixed by #4658
Labels
T: bug Something isn't working

Comments

@tjstum
Copy link

tjstum commented Mar 31, 2025

Describe the bug

Given the following code snippet:

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

black crashes within the reformatting code with the error message:

error: cannot format <string>: Opening paren not found in `leaves`

I reproduced this using the current head of black (black, 25.1.1.dev24+g2c135ed (compiled: no)) and on the online formatter. I can see this on both Linux and macOS and multiple versions of Python (3.12 and 3.13).

I bisected the repro, and it looks like it started breaking in #4016.

Some other things I observed:

  • Using other bracket groups ([] and {}) do not show this issue
  • The magic trailing comma in the argument is important
  • If the comment comes at the end, it's fine. This does not crash:
help(lambda x=(
    "bar",
    # comment
): False)
  • Expanding on the above, comments in the middle of arguments also cause the issue:
help(lambda x=(
    "extremely lengthy argument 1 extremely lengthy argument 1",
    # comment
    "extremely lengthy argument 2 extremely lengthy argument 2",
): False)

To Reproduce

$ black --diff -v -c '''help(lambda x=(
    # comment
    "bar",
): False)
'''
@tjstum tjstum added the T: bug Something isn't working label Mar 31, 2025
@tusharsadhwani
Copy link
Collaborator

Thanks for the git bisect, it is helping a lot right now!

@tusharsadhwani
Copy link
Collaborator

The bug seems to be getting triggered in a completely unrelated part of code though.

Turning maybe_increment_lambda_arguments and maybe_decrement_lambda_arguments into no-ops fixes this crash.

@tusharsadhwani
Copy link
Collaborator

After more digging the bug is likely not in maybe_decrement_lambda_arguments, that's just a side effect.

@siavasha siavasha linked a pull request Apr 26, 2025 that will close this issue
3 tasks
@siavasha
Copy link

Hi there,

I've created a fix for this issue in PR #4658: #4658

In my understanding, the bug occurs because when standalone comments appear within parentheses in lambda default arguments, they create line breaks in Black's AST representation, causing the opening parenthesis to be in a different Line object than the closing parenthesis. When has_magic_trailing_comma tries to find the opening parenthesis, it can't locate it in the current line's leaves, resulting in an uncaught LookupError.

My solution adds a try-except block to catch this specific exception and handle it properly. I've included test cases that cover various scenarios including comments in different positions, multiple comments, nested lambdas with comments, and several edge cases.

I'd appreciate your review when you have a chance. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants