Skip to content

Black crashes when multiple # fmt: skip are added in a multi-part if-clause. #4731

@ranjodhsingh1729

Description

@ranjodhsingh1729

Describe the bug

Black crashes on the following valid python code:

class ClassWithALongName:
    Constant1 = 1
    Constant2 = 2
    Constant3 = 3


def test():
    if (
        "cond1" == "cond1"
        and "cond2" == "cond2"
        and 1 in (
            ClassWithALongName.Constant1,
            ClassWithALongName.Constant2,
            ClassWithALongName.Constant3, # fmt: skip
        ) # fmt: skip
    ):
        return True
    return False

To Reproduce

Just run black on the given code.

The resulting error is:

error: cannot format temp.py: Cannot parse for target version Python 3.9: 15:5:     ): # fmt: skip

Oh no! 💥 💔 💥
1 file failed to reformat.

Environment

  • Black's version: main
  • OS and Python version: Ubuntu 24.04 LTS, Python3.9 (tested on others on playground)

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: crashBlack is crashingF: fmtskipfmt: skip implementation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions