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 conditional inside # fmt: off, depending on comment indentation #3436

Closed
lfurrer opened this issue Dec 13, 2022 · 2 comments · Fixed by #3439
Closed

Crash on conditional inside # fmt: off, depending on comment indentation #3436

lfurrer opened this issue Dec 13, 2022 · 2 comments · Fixed by #3439
Labels
C: crash Black is crashing F: fmtoff fmt: off implementation T: bug Something isn't working

Comments

@lfurrer
Copy link

lfurrer commented Dec 13, 2022

Describe the bug

If a condition statement is wrapped in # fmt: off/# fmt: on, Black crashes with a parse error on the subsequent line.
The crash only occurs if the # fmt: off comment is indented the same as the wrapped condition.

To Reproduce

The following minimal code breaks parsing with Black 22.10 and later:

def do_something(x, y):
    if x:
        return x
    # fmt: off
    elif y:
    # fmt: on
        return y

And run it with these arguments:

$ black test.py

The resulting error is:

error: cannot format test.py: Cannot parse: 7:4: return y

Expected behavior

Black can parse this valid Python snippet regardless of how the # fmt: off comment is indented.
This is the case for Black 22.6 and older.

Environment

  • Black's version: 22.10
  • OS and Python version: Linux/Python 3.8

Additional context

Interestingly, Black doesn't crash if the fmt-off comment is indented one more level.
This is parsed fine:

def do_something(x, y):
    if x:
        return x
        # fmt: off
    elif y:
    # fmt: on
        return y

However, the instruction is also ignored, ie. the # fmt: off command isn't recognised as such.

@lfurrer lfurrer added the T: bug Something isn't working label Dec 13, 2022
@JelleZijlstra JelleZijlstra added F: fmtoff fmt: off implementation C: crash Black is crashing labels Dec 13, 2022
@yilei
Copy link
Contributor

yilei commented Dec 13, 2022

This is a similar case to # fmt: skip applied on the : line (#3148), I'll take a look.

@lfurrer
Copy link
Author

lfurrer commented Dec 14, 2022

@yilei wow, this was fast – thanks!

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

Successfully merging a pull request may close this issue.

3 participants