You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Auto-formatting of pyi files with black produces unexpected results #13341
area-formattingbugIssue identified by VS Code Team member as probable bugneeds spikeLabel for issues that need investigation before they can be worked on.
Extension version (available under the Extensions sidebar): 2020.7.96456
OS and version: MacOS
Python version (& distribution if applicable, e.g. Anaconda): 3.8
Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
Relevant/affected Python packages and their versions: black==19.10b0
Value of the python.languageServer setting: Pylance
Expected behaviour
When 'editor.formatOnSave' is set to true and 'python.formatting.provider' is set to 'black', saving a ".pyi" stub file should auto-format in the same way as a ".py" file.
Actual behaviour
After modifying a ".pyi" file and saving, extra lines are emitted between each method/function, and an extra line is added at the end of the file. Saving the file a second time reformats again and removes the extra lines between method/functions but leaves the extra line at the end of the file. When submitting this type stub file to typeshed, it fails CI because black indicates that there is an extra line at the end of the file.
If I invoke black directly from the terminal tab on the file, it correctly auto-formats the first time — i.e. it doesn't add any extra lines.
This screen shot shows what happens when "format on save" is enabled (after the first save).
This screen shot shows what happens after the second save. Note that there is an extra line left at the bottom.
This screen shot shows what happens when I manually call black directly from the terminal.
Steps to reproduce:
Create or open a type stub file (ending in ".pyi") that contains a class with multiple methods.
Configure your editor for "format on save" with black as the formatter.
Save the file once. Save it a second time.
Reacted by Christopher Dignam, Steve Dignam and Ian Chamberlain
When there are no unsaved changes VSCode will run:
./.venv/bin/black--diff--quiet ./queryset.pyi
Which has the proper file ending so the formatting will change back to the type stub specific style.
So the extension needs to pass the code via stdin to black w/ the --pyi flag when appropriate
or change the tmp file endings to match the file that is being formatted.
Steve Dignam (@sbdchd) Passing content via stdin does help but it also introduces another type of problem. What happens is that the tool no longer has the context based on location to filter out certain things, like say avoid editing library files. So, in this case if you open some site package file, since we are not passing the path to it, black will format it. So we need to update the extension to filter those out.
So if we go stdin approach, then we need to filter the non-workspace files out, and then call the formatting tool with content passed via stdin.
Gotcha, It looks like the --pyi flag can also used with formatting a file directly
so I think a check of the file name should be sufficient when creating the args for black
area-formattingbugIssue identified by VS Code Team member as probable bugneeds spikeLabel for issues that need investigation before they can be worked on.
Activity
karthiknadig commentedon Aug 11, 2020
I can repro this. Looks like something goes wrong when we try to save
.pyi. We seem to flip-flop between these two states.Here is sample A:
Here is sample B:
sbdchd commentedon Nov 15, 2020
I was investigating this some more and I think it's from the temp files used for formatting unsaved buffers.
When saving a modified buffer, or by formatting the buffer manually via
Format Document, the following command is run:When there are no unsaved changes VSCode will run:
Which has the proper file ending so the formatting will change back to the type stub specific style.
So the extension needs to pass the code via stdin to
blackw/ the--pyiflag when appropriateor change the tmp file endings to match the file that is being formatted.
karthiknadig commentedon Nov 15, 2020
Steve Dignam (@sbdchd) Passing content via
stdindoes help but it also introduces another type of problem. What happens is that the tool no longer has the context based on location to filter out certain things, like say avoid editing library files. So, in this case if you open some site package file, since we are not passing the path to it, black will format it. So we need to update the extension to filter those out.So if we go
stdinapproach, then we need to filter the non-workspace files out, and then call the formatting tool with content passed viastdin.sbdchd commentedon Nov 15, 2020
Gotcha, It looks like the
--pyiflag can also used with formatting a file directlyso I think a check of the file name should be sufficient when creating the args for
blacksee: #14736
fix: formatting .pyi files with black (#14736)
karthiknadig commentedon Nov 30, 2020
#14736 Partially addresses the .pyi issue. The fix only works with files that have .pyi as the extension and the formatting is applied on save.
This does not work with the case where the formatting is applied to files that are not saved.
karrtikr commentedon Dec 21, 2022
Closing as we now have a new black extension which does not save files while formatting, so won't have this problem.