Skip to content

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

Closed as not planned

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.

Activity

  1. added
    bug
    triage-needed
    on Aug 7, 2020
  2. ghost removed
    triage-needed
    on Aug 10, 2020
  3. karthiknadig commented on Aug 11, 2020

    @karthiknadig
    Member

    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:

    --- c:\\GIT\\s p\\python-type-stubs\\freezegun\\api.pyi.a7adf3e30efc0d8ca8d4ee0bc707865a.tmp\t2020-08-11 07:31:13.815015 +0000\r\n
    +++ c:\\GIT\\s p\\python-type-stubs\\freezegun\\api.pyi.a7adf3e30efc0d8ca8d4ee0bc707865a.tmp\t2020-08-11 07:31:20.196630 +0000\r\n
     class TickingDateTimeFactory(object):\r\n
         def __init__(self, time_to_freeze: datetime.datetime, start: datetime.datetime) -> None:\r\n
             self.time_to_freeze: datetime.datetime = ...\r\n
             self.start: datetime.datetime = ...\r\n
    -    def __call__(self) -> datetime.datetime: ...\r\n
    +\r\n
    +    def __call__(self) -> datetime.datetime:\r\n
    +        ...\r\n
    +\r\n
     \r\n

    Here is sample B:

    --- c:\\GIT\\s p\\python-type-stubs\\freezegun\\api.pyi\t2020-08-11 07:27:16.662910 +0000\r\n
    +++ c:\\GIT\\s p\\python-type-stubs\\freezegun\\api.pyi\t2020-08-11 07:28:58.453582 +0000\r\n
     class TickingDateTimeFactory(object):\r\n
         def __init__(self, time_to_freeze: datetime.datetime, start: datetime.datetime) -> None:\r\n
             self.time_to_freeze: datetime.datetime = ...\r\n
             self.start: datetime.datetime = ...\r\n
    -\r\n
    -    def __call__(self) -> datetime.datetime:\r\n
    -        ...\r\n
    -\r\n
    +    def __call__(self) -> datetime.datetime: ...\r\n
     \r\n
  4. added
    needs spike
    and removed on Aug 11, 2020
  5. sbdchd commented on Nov 15, 2020

    @sbdchd

    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:

    ./.venv/bin/black --diff --quiet ./queryset.pyi.26c0667c29728299036ec32a0f6f7729.tmp

    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.

  6. karthiknadig commented on Nov 15, 2020

    @karthiknadig
    Member

    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.

  7. sbdchd commented on Nov 15, 2020

    @sbdchd

    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

    see: #14736

  8. ghost removed
    needs spike
    on Nov 30, 2020
  9. ghost added
    triage-needed
    on Nov 30, 2020
  10. karthiknadig commented on Nov 30, 2020

    @karthiknadig
    Member

    #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.

  11. added
    needs spike
    on Dec 1, 2020
  12. ghost removed
    triage-needed
    on Dec 1, 2020
  13. karrtikr commented on Dec 21, 2022

    @karrtikr

    Closing as we now have a new black extension which does not save files while formatting, so won't have this problem.

  14. locked as resolved and limited conversation to collaborators on Jan 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-formattingbugneeds spike

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions