psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
38.87k stars 2.45k forks source link

Black produced code that is not equivalent to the source (mixed tabs/spaces) #2469

Open wsot opened 3 years ago

wsot commented 3 years ago

Describe the bug

When attempting to format the attached (problematic) file, black outputs INTERNAL ERROR: Black produced code that is not equivalent to the source

To Reproduce

  1. Take the attached file
  2. Run Black on it with these arguments run black -l 120 --target-version py38 src (where src is the parent directory of the file)
  3. See error error: cannot format <my file path>/_instrumentation.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/f8/9fz_qgqs5d7ctybtrblxzkpc0000gn/T/blk_3xx4g81a.log

Expected behavior

Either the file gets correctly formatted, or an error message is returned that the file cannot be formatted

Environment (please complete the following information):

Does this bug also happen on main?

Using the online formatter, the following error is produced:

cannot use --safe with this file; failed to parse source file.  AST error message: 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]`.

Additional context

The attached file is not valid because it mixes tabs and spaces, so it is reasonable that it doesn't format - it's the error feedback that is probably the real issue

Log file (taken from the 21.8b0 run) blk_eik4al6v.log

File being formatted example.txt

FelixEnescu commented 3 years ago

The issue also appears if the tabs are inside a string. See attached example. in_string.txt