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

fmt on/off raises internal error when fmt: off in multi-level intended code block #2842

Closed
izikeros opened this issue Jan 31, 2022 · 2 comments
Labels
F: fmtoff fmt: off implementation R: duplicate This issue or pull request already exists T: bug Something isn't working

Comments

@izikeros
Copy link

Description

Black raises error when formatting code that includes specific combination of fmt: off and fmt: on commands. The bug occurs when fmt: off is before intended block and fmt: on inside intended block. Bug occurs only in multi-level indentation (see: examples below)

To Reproduce
Try to format file with the code:

for i in range(2):
    # fmt: off
    for j in range(2):
        # fmt: on
        pass

Note: similar code but with single-level of indent not raises any error:

# fmt: off
for j in range(2):
    # fmt: on
    pass

And run it with these arguments:

$ black file.py

The resulting error is:

error: cannot format file.py: INTERNAL ERROR: Black produced code that is not equivalent to the source on pass 1. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: /var/folders/97/sr1jt8qx6b90q4hqfsv06qbc0000gq/T/blk_0vp6bvi8.log

Expected behavior

Black should format file without raising error.

Environment

  • Black's version: 22.1.0
  • OS and Python version: macOS 12.2 (21D49), Python 3.9.10
@izikeros izikeros added the T: bug Something isn't working label Jan 31, 2022
@felix-hilden
Copy link
Collaborator

Hi! As documented, # fmt: off / on have to be on the same level of indentation. We have discussed fixing this in #2140 (#569 also related), so I'll close this as a duplicate.

@felix-hilden felix-hilden added F: fmtoff fmt: off implementation R: duplicate This issue or pull request already exists labels Jan 31, 2022
@felix-hilden
Copy link
Collaborator

Verified that this doesn't happen on the compiled version either. @izikeros If this is indeed the case, please submit another issue detailing the problem!

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

2 participants