google / google/yapf

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

Open
#1,008 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
14k
Forks
904
PR merge metrics
No merged PRs in 30d

Description

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:

```py
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:

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.