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

Crash on formatting case case syntax when line length set to be small #4280

Open
rdrll opened this issue Mar 15, 2024 · 2 comments
Open

Crash on formatting case case syntax when line length set to be small #4280

rdrll opened this issue Mar 15, 2024 · 2 comments
Labels
C: crash Black is crashing T: bug Something isn't working

Comments

@rdrll
Copy link
Contributor

rdrll commented Mar 15, 2024

Describe the bug

When using the match-case block with a case case statement (syntax allowed since Python 3.10), Black fails to parse the code if the line length is set to a small number.

To Reproduce

Consider the following code:

match test:
    case case if True:
        pass

This code can be correctly formatted by Black when the line length is reduced to 14, as shown below (playground link)

match test:
    case (
        case
    ) if True:
        pass

However, when the line length is further decreased to 13 or below, Black will not be able to parse the code, (see playground link)

This issue can also be reproduced locally:

PS > cat temp.py
match test:
    case case if True:
        pass

PS > python -m black temp.py --line-length 13
error: cannot format temp.py: Cannot parse: 5:8:         if True

Oh no! 💥 💔 💥
1 file failed to reformat.

PS > python -m black temp.py --line-length 1
error: cannot format temp.py: Cannot parse: 3:8:         if True

Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior

With line length set to be lower than 13, the output format should remain as

match test:
    case (
        case
    ) if True:
        pass

Environment

  • Local environment: Windows
PS > black --version
black, 24.2.0 (compiled: yes)
Python (CPython) 3.10.7
  • Black Playground version: current stable v24.1.0
@rdrll rdrll added the T: bug Something isn't working label Mar 15, 2024
@JelleZijlstra JelleZijlstra changed the title Crush on formatting case case syntax when line length set to be small Crash on formatting case case syntax when line length set to be small Mar 15, 2024
@JelleZijlstra JelleZijlstra added the C: crash Black is crashing label Mar 15, 2024
@hauntsaninja
Copy link
Collaborator

Thanks for the issue! It looks like this is already fixed in preview style, by #4214

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Mar 21, 2024

Hm actually no that's not quite right, #4214 just reduces the line length at which this crashes. Black wants to do something like:

match test:
    case case (
        if True
    ):
        pass

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

No branches or pull requests

3 participants