Skip to content
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

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

Open
tjstum opened this issue Mar 31, 2025 · 3 comments
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.

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

No branches or pull requests

2 participants