Skip to content

Crash with fmt: off + decorator #2232

@rdemetrescu

Description

@rdemetrescu

Describe the bug

I am getting an exception error: cannot format test_black.py: INTERNAL ERROR: Black produced code that is not equivalent to the source. within a very specific scenario.

To Reproduce:

  1. Take this file 'test_black.py'
import pytest


class TestDummy:
    # fmt: off
    @pytest.mark.parametrize("param1", ["foo", "bar"])  # line A
    # fmt: on
    @pytest.mark.skip("special line")  # line B
    def test_dummy(self, param1):
        print(param1)


print("if you remove me, it will work too")  # line C
  1. Run Black on it
$ black test_black.py
  1. See error
error: cannot format test_black.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: /tmp/blk_s3rf0vdz.log
Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior

The file is already well-formatted. I was expecting no error on the console.

Environment:

  • Version: 21.5b1 or main
  • OS and Python version: Linux with Python 3.8.5
  • also on OS and Python version: macOS Darwin 19.6.0 with Python 3.8.0

Does this bug also happen on main?:

Yes, it happens.

Additional context

This file has a minimal setup to reproduce the error:

  • if # fmt: off (above line with comment # line A) is removed OR...
  • if the line with the comment # line B is removed (only for main and for macOS) OR...
  • if the line with comment # line C is removed

then Black run without any error.

Here is the attached log file:
blk_zpx9dsc4.log

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: invalid codeBlack destroyed a valid Python fileF: fmtofffmt: off implementationT: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions