Fix not emitting newline markers in patches#4745
Merged
Liam-DeVoe merged 1 commit intoMay 27, 2026
Merged
Conversation
`difflib.unified_diff` doesn't emit the marker that git requires when either side of the diff lacks a trailing newline, so `git apply` rejects the resulting patch as corrupt. When both sides' differing last lines lack newlines, the raw output also concatenates the `-` and `+` lines into a single mashed line. Post-process the diff to terminate any no-newline line with `\n` and append the marker.
Zac-HD
approved these changes
May 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4744.
Claude-written description
make_patch()inhypothesis/extra/_patching.pyusesdifflib.unified_diff, which doesn't emit the\ No newline at end of filemarker that git requires when either side of the diff lacks a trailing newline. The pytest plugin's resulting patch is therefore rejected bygit applyas corrupt.Worse: when both sides' differing last lines lack newlines, the raw
unified_diffoutput concatenates the-and+lines into a single mashed line (e.g.-last = old+last = new), so the patch isn't just missing an annotation, it's structurally broken.The fix post-processes the diff output: for any line that doesn't end with
\n, terminate it and append the marker. This handles all five permutations (both sides have/lack newline, equal/differing last lines).Two new tests in
tests/patching/test_patching.pyassert exact patch text for the context-line and changed-line cases.