Skip to content

INTERNAL ERROR: Black produced invalid code on pass 1: unmatched ')' #2478

@Jasha10

Description

@Jasha10

Describe the bug

I accidentally got the order of my # fmt: on and # fmt: off comments backwards.
This seems to cause an INTERNAL ERROR under some circumstances

To Reproduce

A python file to trigger the bug:

# tmp.py
def foo():
    arr = (
       # fmt: on
       (3833567325051000, 5, 1, 2, 4229.25, 6, 0),
       # fmt: off
    )

Note that usually # fmt: off will come before # fmt: on, but here they are reversed.

Here is the command-line output:

$ black tmp.py
error: cannot format tmp.py: INTERNAL ERROR: Black produced invalid code on pass 1: unmatched ')' (<unknown>, line 8). Please report a bug on https://github.com/psf/black/issues.  This invalid output might be helpful: /tmp/blk_rm36cb4t.log
Oh no! 💥 💔 💥
1 file failed to reformat

Here are the contents of /tmp/blk_rm36cb4t.log:

  File "/home/jasha10/miniconda3/envs/pysc/lib/python3.9/site-packages/black/__init__.py", line 1210, in assert_equivalent
    dst_ast = parse_ast(dst)
  File "/home/jasha10/miniconda3/envs/pysc/lib/python3.9/site-packages/black/parsing.py", line 138, in parse_ast
    raise SyntaxError(first_error)
# tmp.py
def foo():
    arr = (
        # fmt: on
        (3833567325051000, 5, 1, 2, 4229.25, 6, 0),
        # fmt: off
    )
    )

Expected behavior
No internal error

Environment (please complete the following information):

black, version 21.8b0
CPython 3.9.7
OS: Ubuntu 18.04

Does this bug also happen on main?

Yes

  1. Use the online formatter at https://black.vercel.app/?version=main, which will use
    the latest main branch.

Here is the output from the online formatter:

INTERNAL ERROR: Black produced invalid code on pass 1: The requested source code has invalid Python 3 syntax.
If you are trying to format Python 2 files please reinstall Black with the 'python2' extra: `python3 -m pip install black[python2]`.. Please report a bug on https://github.com/psf/black/issues.  This invalid output might be helpful: /tmp/blk_ojxou1n8.log

  File "/var/task/black/__init__.py", line 986, in assert_equivalent
    dst_ast = parse_ast(dst)
  File "/var/task/black/parsing.py", line 125, in parse_ast
    raise SyntaxError(
# tmp.py
def foo():
    arr = (
        # fmt: on
        (3833567325051000, 5, 1, 2, 4229.25, 6, 0),
        # fmt: off
    )
    )

Additional context
The output from the online formatter says "The requested source code has invalid Python 3 syntax."
I believe that this claim is incorrect; the syntax looks fine to me, and the code runs in the python interpreter without a problem.

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