Skip to content

Commit 30c1a54

Browse files
committed
Fix --diff output when encountering EOF
`split("\n")` includes a final empty element `""` if the final line ends with `\n` (as it should for POSIX-compliant text files), which then became an extra `"\n"`. `splitlines()` solves that, but there's a caveat, as it will split on other types of line breaks too (like `\r`), which may not be desired. Fixes #526.
1 parent 9ed2542 commit 30c1a54

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

black.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3876,8 +3876,8 @@ def diff(a: str, b: str, a_name: str, b_name: str) -> str:
38763876
"""Return a unified diff string between strings `a` and `b`."""
38773877
import difflib
38783878

3879-
a_lines = [line + "\n" for line in a.split("\n")]
3880-
b_lines = [line + "\n" for line in b.split("\n")]
3879+
a_lines = [line + "\n" for line in a.splitlines()]
3880+
b_lines = [line + "\n" for line in b.splitlines()]
38813881
return "".join(
38823882
difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
38833883
)

tests/data/blackd_diff.diff

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--- [Deterministic header]
22
+++ [Deterministic header]
3-
@@ -1,7 +1,6 @@
3+
@@ -1,6 +1,5 @@
44
-def abc ():
55
- return ["hello", "world",
66
- "!"]
@@ -9,6 +9,5 @@
99

1010
-print( "Incorrect formatting"
1111
-)
12-
12+
+
1313
+print("Incorrect formatting")
14-
+

0 commit comments

Comments
 (0)