Skip to content

Black crashes when disabling fmt in some parts of the code #3184

@masipcat

Description

@masipcat

Describe the bug

Black crashes when disabling fmt in some parts of the code

To Reproduce

For example, take this code:

class A:
    async def call(param):
        if param:
            # fmt: off
            if param[0:4] in (
                "ABCD", "EFGH"
            ):
                # fmt: on
                return 1

            elif param[0:4] in ("ZZZZ",):
                return 2

            elif param[0:4] in ("YYYY",):
                return 3

        return 4

If the fmt:off/on is moved to the elif, then it works:

class A:
    async def call(param):
        if param:
            if param[0:4] in (
                "ABCD", "EFGH"
            ):
                return 1

            # fmt: off
            elif param[0:4] in (
                "ZZZZ",
            ):
                # fmt: on
                return 2

            elif param[0:4] in ("YYYY",):
                return 3

        return 4

The resulting error is:

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

Expected behavior

To format the code

Environment

Black (vercel): https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AHUALtdAD2IimZxl1N_WlXnON2nzNApqtITJKNM5khekDYCHqoZycPfWzsPujeCAa9Rpjc9QNsUFfcNW21kfXWi3Ds1zEobtzgrlyJFv6pJgbr3hjVNgtP72qI38X2kZz6KEWMeQHyjVFNfYPRCWhXM_df-ORVEnAhEjzVHVfUOGnT6eg7CFQGFmjFuPqN9w6a87oJlA5IKn438pwNRwAkPWitXFDzX-irhc11HxGrYugCMb6BF-LvIjSN0daMSCgAAAFoGUhUf-a1_AAHXAdUDAAAIJsLiscRn-wIAAAAABFla

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: fmtofffmt: off implementationT: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions