Skip to content

print_diff / diff does not include new line at the end of the file #1008

@masad-frost

Description

@masad-frost

Using normal mode in yapf adds new lines to the formatted code, but when using the diff option new lines at the end of file are not included in the patch. Maybe the diff is generated based on unformatted code that has EOF newline added.

You can try it here https://replit.com/@replitfaris/yapf-newline-patch

source:
from yapf.yapflib.yapf_api import FormatCode


unformatted_code = "f(a=1, b=2)"
print("unformatted code")
print(repr(unformatted_code))
print("")

formatted_code, _ = FormatCode(unformatted_code)

print("formatted code:")
print(repr(formatted_code))
print("")

diff, _ = FormatCode(unformatted_code, print_diff=True)

"""
This is empty but should be:

--- <unknown>   (original)
+++ <unknown>   (reformatted)
@@ -1 +1 @@
-f(a=1, b=2)
\ No newline at end of file
+f(a=1, b=2)
"""
print("diff:")
print(diff)
print("")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions