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 when fmt:off and on do not match #924

Closed
ppwwyyxx opened this issue Jul 10, 2019 · 2 comments
Closed

INTERNAL ERROR when fmt:off and on do not match #924

ppwwyyxx opened this issue Jul 10, 2019 · 2 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

@ppwwyyxx
Copy link

Howdy! Sorry you're having trouble. To expedite your experience,
provide some basics for me:

Operating system:
Python version: 3.7
Black version: black, version 19.3b0
Does also happen on master: yes

class A():
    def __init__(self, cfg):
        # fmt: off
        b = 1
        # fmt: off

class B():
    def __init__(self, cfg):
        # fmt: off
        a = 1
        # fmt: on
        assert a == cfg

When running locally, I observe:

error: cannot format a.py: INTERNAL ERROR: Black produced invalid code: expected an indented block (<unknown>, line 10). Please report a bug on https://github.com/ambv/black/issues.  This invalid output might be helpful: /tmp/blk_9w1d83ne.log
All done! 💥 💔 💥
1 file failed to reformat.

when running online with master, I observe wrong indentation in the output:
0710-16:22:43

@zsol zsol added T: bug Something isn't working C: invalid code Black destroyed a valid Python file labels Jul 22, 2019
@mcsitter
Copy link
Contributor

mcsitter commented Sep 8, 2020

This still happens. I do not know exactly how ast parsing works, but maybe i can add some info so this can be resolved. The error message is also the same.

The invalid output as shown in the log that gets linked in the error message includes a traceback and may be more than shown above:
grafik

The _ast27.parse function is implemented in CPython and lies in the _ast27.cpython file in the typed_ast site-package. I do not know whether the code in the log is coming from black or from typed_ast. I have trouble looking into its code base. I found however this comment in typed_ast: Python 3.8.

I reproduced the issue with the current pipfile defined on master, which is on Python version 3.8, and on 3.7 by running it in a environment created by pipenv install --python 3.7 --dev. Both create the same error message and log file.

The same error message appears on this example as well:

def foo():
    # fmt: off
    pass
bar = 1

In the log is no traceback however, just some output i do not understand. Instead of assigning 1 to bar you can also insert a function or class definition or any other assignment. I do now know whether this is still in the scope of the reported bug. Both appear to be issues on parsing the code.

Here is what i think happens: The parts with # fmt: off do not get passed to the ast parsing and this can mess things up. Maybe one could make sure that # fmt: on is inserted at the end of function and class definitions since adding # fmt: on or '# fmt: off' or combinations of those into a following class or function definition, does not solve the issue.

If anyone can help out how to tackle this i would greatly appreciate it. Even finding out whether it is actually a problem with black or the ast parser would be nice! It seems to be maintained quite regularly.

@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label Sep 18, 2020
@ichard26
Copy link
Collaborator

Duplicate of #569.

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

4 participants