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

INTERNAL ERROR on mismatched # fmt comments #1341

Closed
inglesp opened this issue Apr 10, 2020 · 1 comment
Closed

INTERNAL ERROR on mismatched # fmt comments #1341

inglesp opened this issue Apr 10, 2020 · 1 comment
Labels
R: duplicate This issue or pull request already exists T: bug Something isn't working

Comments

@inglesp
Copy link

inglesp commented Apr 10, 2020

Describe the bug INTERNAL ERROR on mismatched # fmt comments

To Reproduce Steps to reproduce the behavior:

Run black over a file containing mismatched # fmt comments inside functions.

$ cat bad.py 
def f1():
    pass
    # fmt: off
    # fmt: off


def f2():
    pass
    # fmt: off
    # fmt: on

$ black bad.py
error: cannot format bad.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: /var/folders/fm/_0l2_jwj0cs6mz43zdq91gfw0000gn/T/blk_62sexy11.log
Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior Black should not crash, and should point out the mismatched # fmt comments.

Environment (please complete the following information):

  • Version: 19.10b0
  • OS and Python version: OSX/Python 3.8.0

Does this bug also happen on master? To answer this, you have two options:

Yes, with 19.10b1.dev48+g959848c on my machine.

But with the online formatter (running 959848c) it doesn't crash, but produces something incorrect:

image

def f1():
    pass
    # fmt: off
    # fmt: off

    def f2():
        pass
        # fmt: off
        # fmt: on

Additional context I have attached the debug output.

blk_62sexy11.log

@inglesp inglesp added the T: bug Something isn't working label Apr 10, 2020
@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label Aug 4, 2020
@ichard26
Copy link
Collaborator

ichard26 commented Aug 4, 2020

The behaviour seen here signals that this is a duplicate of #569. If you move the formatting control comments to the global scope, Black won't produce bad code.

(black-rHKUX7ap) R:\Programming\black>type test.py
# fmt: off
# fmt: off
some_var = [1, 3,
 3 ,124 , 125]

def foo(): pass

def baz():
    return "I'm baz!"

# fmt: off
# fmt: on

def bar():
    raise NotImplementedError("It's a test function duh!")

another_misformatted_var = ('hello world', [1, 2
], 53,
"placeholder text")

(black-rHKUX7ap) R:\Programming\black>black test.py --diff --color
--- test.py     2020-08-04 00:25:30.870797 +0000
+++ test.py     2020-08-04 00:28:10.698073 +0000
@@ -9,11 +9,11 @@
     return "I'm baz!"

 # fmt: off
 # fmt: on

+
 def bar():
     raise NotImplementedError("It's a test function duh!")

-another_misformatted_var = ('hello world', [1, 2
-], 53,
-"placeholder text")
+
+another_misformatted_var = ("hello world", [1, 2], 53, "placeholder text")
would reformat test.py
All done! ✨ 🍰 ✨
1 file would be reformatted.

Thanks for filing this bug report!

Side-note: if someone else is interested having Black notice and warn about mismatched formatting control comments, please file a new (enhancement) issue.


Environment:

  • Black version: master (b59a524)
  • Python version: CPython 3.8.1
  • OS version: Windows 10 Home Build 18383

@ichard26 ichard26 closed this as completed Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R: duplicate This issue or pull request already exists T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants