Skip to content

Auto-formatting of pyi files with black produces unexpected results #13341

@erictraut

Description

@erictraut

Environment data

  • VS Code version: 1.47.3
  • 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).
image

This screen shot shows what happens after the second save. Note that there is an extra line left at the bottom.
Screen Shot 2020-08-07 at 8 15 52 AM

This screen shot shows what happens when I manually call black directly from the terminal.
Screen Shot 2020-08-07 at 8 11 57 AM

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-formattingbugIssue identified by VS Code Team member as probable bugneeds spikeLabel for issues that need investigation before they can be worked on.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions