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: Black produced invalid code: invalid syntax (<unknown>, line 37) #2030

Closed
traditio opened this issue Mar 9, 2021 · 5 comments
Labels
C: invalid code Black destroyed a valid Python file R: duplicate This issue or pull request already exists T: bug Something isn't working

Comments

@traditio
Copy link

traditio commented Mar 9, 2021

Black daemon throws an internal error on the following file.
Error: INTERNAL ERROR: Black produced invalid code: invalid syntax (<unknown>, line 37)

  • Version: black, version 20.8b1
  • OS Windows
  • Conda environment: conda version : 4.9.2
  • Python verison: 3.8.5.final.0

Log file content:

  File "c:\tools\miniconda3\envs\tools\lib\site-packages\black\__init__.py", line 6141, in assert_equivalent
    dst_ast = parse_ast(dst)
  File "c:\tools\miniconda3\envs\tools\lib\site-packages\black\__init__.py", line 6052, in parse_ast
    return ast27.parse(src)
  File "c:\tools\miniconda3\envs\tools\lib\site-packages\typed_ast\ast27.py", line 50, in parse
    return _ast27.parse(source, filename, mode)
import unittest
from polymer.core.exceptions import short_callstack


def _failing_test_method():
    num = 5 / 0


class FormatExceptionsTest(unittest.TestCase):
    def test_short_callstack(self):
        try:
            _failing_test_method()
            self.fail("Not expected to pass")
        except Exception as exc:
            self.assertRegexpMatches(short_callstack(exc), "test_exceptions_format.py:\d,_failing_test_method,num = 5 / 0$")

        try:
            self._failing_test_method()
            self.fail("Not expected to pass")
        except Exception as exc:
            self.assertRegexpMatches(short_callstack(exc), "test_exceptions_format.py:\d+,_failing_test_method,num = 5 / 0$")

        try:
            # keep multiline for test purpose
            num = 5 / 0
            self.fail("Not expected to pass")
        except Exception as exc:
            self.assertRegexpMatches(short_callstack(exc), "test_exceptions_format.py:2\d+,test_short_callstack,0$")

    def test_short_callstack_multiline(self):
        try:
            # keep multiline for test purpose
            # fmt: off
            num = 5 /\
                  0
            self.fail("Not expected to pass")
            except Exception as exc:
                self.assertRegexpMatches(short_callstack(exc), "test_exceptions_format.py:2\d+,test_short_callstack,0$")

        def _failing_test_method(self):
            num = 5 / 0
@traditio traditio added the T: bug Something isn't working label Mar 9, 2021
@JelleZijlstra
Copy link
Collaborator

Could you share the initial contents of the file?

@traditio
Copy link
Author

It shared in the bug description under "log file content"
Also the issue reproduces on shorter file:

class Test():
    def test(self):
        try:
            # keep multiline for test purpose
            # fmt: off
            num = 5 /\
                  0
        except Exception as exc:
            pass

command line output:

$ black --version
black, version 20.8b1

$ black test.py
error: cannot format test.py: INTERNAL ERROR: Black produced invalid code: invalid syntax (<unknown>, line 8). Please report a bug on https://github.com/psf/black/issues.  This invalid output might be helpful: C:\Users\dmipatra\AppData\Local\Temp\blk_wf6xwmwy.log
Oh no! � � �
1 file failed to reformat.

@JelleZijlstra
Copy link
Collaborator

Thanks, I think the log file shows Black's output, not input. The issue is that there is no # fmt: on to match the fmt: off; the documentation (https://github.com/psf/black#the-black-code-style) specifies that they have to both be present at the same level of indentation. I suppose ideally we still shouldn't crash.

@ichard26
Copy link
Collaborator

This is another case of #569. Proof from the Black Playground

@ichard26 ichard26 added the C: invalid code Black destroyed a valid Python file label Mar 10, 2021
@JelleZijlstra
Copy link
Collaborator

Closing as a duplicate of #569.

@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label May 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: invalid code Black destroyed a valid Python file R: duplicate This issue or pull request already exists T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants