Skip to content

Unhelpful error message if you forget # fmt: on after a # fmt: off #1817

@knatten

Description

@knatten

Describe the bug

If you forget to add # fmt: on after a # fmt: off, you get an unhelpful error message.

To Reproduce

  1. Take this file repro.py
def f():
    # fmt: off
    pass

if True:
    pass
  1. Run black --check repro.py

  2. See error:

error: cannot format repro.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_k081_p1l.log
Oh no! 💥 💔 💥
1 file would fail to reformat.

Expected behavior
I would expect that this either works (and it just ignores the rest of the file), or that you get an error message telling you that you need to add # fmt: on somewhere.

Environment (please complete the following information):

  • Version: 20.8b1
  • OS and Python version: Ubuntu 20.04, Python 3.8.5

Does this bug also happen on master?
Yes, it happens on https://black.now.sh/?version=master

Additional context

Here's the output of /tmp/blk_k081_p1l.log:

--- src
+++ dst
@@ -14,26 +14,26 @@
             None,  # NoneType
         )  # /arguments
       body=
         Pass(
         )  # /Pass
+        If(
+          body=
+            Pass(
+            )  # /Pass
+          orelse=
+          test=
+            Constant(
+              value=
+                True,  # bool
+            )  # /Constant
+        )  # /If
       decorator_list=
       name=
         'f',  # str
       returns=
         None,  # NoneType
       type_comment=
         None,  # NoneType
     )  # /FunctionDef
-    If(
-      body=
-        Pass(
-        )  # /Pass
-      orelse=
-      test=
-        Constant(
-          value=
-            True,  # bool
-        )  # /Constant
-    )  # /If
   type_ignores=
 )  # /Module

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions