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

Multiline inconsistent indented (inconsistent scope) # fmt: on / # fmt: off fails: Black produced code that is not equivalent to the source on pass 1 #2745

Closed
SamuelMarks opened this issue Jan 4, 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

@SamuelMarks
Copy link

Describe the bug

I am writing my own Concrete Syntax Tree parser/emitter in Python, but I am using black as my code formatter so I want black to ignore the 'fancy' Python syntax I am trying to support with my parser. So I've got a bunch of badly formatted Python code that I want to support with my parser.

To Reproduce

def add1(foo):
    def adder(a: int, b: int) -> int:
        # fmt: off
        res: \
            int \
            = a + b
        return res

    r = (
        add(foo, 1)
        or
        adder(foo, 1)
    )
    # fmt: on

Expected behavior

It should work

Actual behavior

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: /tmp/blk_6ciutkwa.log

Environment

https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AE_AKFdAD2IimZxl1N_WlbvK5V8IsyYXSZYzCxvB11Od7QFS_cUQRnVWTnEOnFGqeIVOh-6xpWYQw80rntIdUcNTZAqIYyewDaXyz571F0uxel5QXJBPZz4vzo0EFPhMsonxVlUzlqOg0_Xb1km-YJFCPRioooVe1ft1ydHHh6KUs4CK4ZdWV7pO9zBDSqDJun-2hPLYtedLWs2Bub_aZDRwt75JGqiAAAAAMTZEyWxsHatAAG9AcACAACux6gtscRn-wIAAAAABFla

@SamuelMarks SamuelMarks added the T: bug Something isn't working label Jan 4, 2022
@ichard26 ichard26 added the F: fmtoff fmt: off implementation label Jan 4, 2022
@ichard26
Copy link
Collaborator

ichard26 commented Jan 4, 2022

This isn't supported as of right now:

It doesn’t reformat blocks that start with # fmt: off and end with # fmt: on, or lines that ends with # fmt: skip. # fmt: on/off have to be on the same level of indentation. It also recognizes YAPF’s block comments to the same effect, as a courtesy for straddling code.

I'm open to supporting differently indented fmt: off/on but I make no promises on how feasible it may be. ALTHOUGH Black should really be treating the # fmt: off as unterminated and not generate invalid code.

@JelleZijlstra
Copy link
Collaborator

I think this is a duplicate of #569.

For your use case, I'd suggest just not running Black on your test cases.

@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label Jan 7, 2022
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