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

Black crashes when "fmt: off/on" is used with "with" expression #3236

Closed
jakub-m opened this issue Aug 22, 2022 · 5 comments
Closed

Black crashes when "fmt: off/on" is used with "with" expression #3236

jakub-m opened this issue Aug 22, 2022 · 5 comments
Labels
F: fmtoff fmt: off implementation R: duplicate This issue or pull request already exists T: bug Something isn't working

Comments

@jakub-m
Copy link

jakub-m commented Aug 22, 2022

Describe the bug

Black crashes when "fmt: off/on" is used with "with" expression.

To Reproduce

For example, take this code:

# file.py
def do_something():
    # fmt: off
    with open('foo') as foo,\
         open('bar') as bar:
        # fmt: on
        raise SomeError()


class SomeError(Exception):
    pass

And run it with these arguments:

black file.py

or check in black console online HERE.

The resulting error is:

error: cannot format black_bug.py: INTERNAL ERROR: Black produced code that is not equivalent to the source. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: ...

Expected behavior

Should not crash. I use "fmt: off" and "fmt: on" to prevent moving multiple "with" into one a single line.

Environment

% black --version
black, 22.6.0 (compiled: yes)
Python (CPython) 3.9.13

MacOS

@jakub-m jakub-m added the T: bug Something isn't working label Aug 22, 2022
@JelleZijlstra JelleZijlstra added the F: fmtoff fmt: off implementation label Aug 22, 2022
@felix-hilden
Copy link
Collaborator

Hi, fmt on/off need to be on the same indentation level. This is an issue in #569, and we intend to fail gracefully in this case #2985 so that the error message is clearer. I'll close this as a duplicate!

@felix-hilden felix-hilden added the R: duplicate This issue or pull request already exists label Aug 29, 2022
@jakub-m
Copy link
Author

jakub-m commented Aug 29, 2022

@felix-hilden I don't think this is a duplicate. The example also fails when I move the directives to the same indentation level:

# file.py
def do_something():
    # fmt: off
    with open('foo') as foo,\
         open('bar') as bar:
    # fmt: on
        raise SomeError()


class SomeError(Exception):
    pass

or

# file.py
def do_something():
# fmt: off
    with open('foo') as foo,\
         open('bar') as bar:
# fmt: on
        raise SomeError()


class SomeError(Exception):
    pass

or

# file.py
def do_something():
        # fmt: off
    with open('foo') as foo,\
         open('bar') as bar:
        # fmt: on
        raise SomeError()


class SomeError(Exception):
    pass

@felix-hilden
Copy link
Collaborator

None of them are truly on the same indentation level in code. Try:

# fmt: off
with ...:
    pass
# fmt: on

@jakub-m
Copy link
Author

jakub-m commented Aug 30, 2022

Yes, that works, thanks. But that was not the intention. I want to format the body of the with block, I just don't want to format the multi-line with line. I want to skip formatting for a single line, not the whole block.

@felix-hilden
Copy link
Collaborator

Yes, I get that. But this is how fmt off needs to be used. We've got fmt: skip for one-line skips, perhaps that'll help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: fmtoff fmt: off implementation R: duplicate This issue or pull request already exists T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants