Skip to content

Fix not emitting newline markers in patches#4745

Merged
Liam-DeVoe merged 1 commit into
HypothesisWorks:masterfrom
Liam-DeVoe:fix-patch-no-trailing-newline
May 27, 2026
Merged

Fix not emitting newline markers in patches#4745
Liam-DeVoe merged 1 commit into
HypothesisWorks:masterfrom
Liam-DeVoe:fix-patch-no-trailing-newline

Conversation

@Liam-DeVoe
Copy link
Copy Markdown
Member

@Liam-DeVoe Liam-DeVoe commented May 26, 2026

Fixes #4744.

Claude-written description

make_patch() in hypothesis/extra/_patching.py uses difflib.unified_diff, which doesn't emit the \ No newline at end of file marker that git requires when either side of the diff lacks a trailing newline. The pytest plugin's resulting patch is therefore rejected by git apply as corrupt.

Worse: when both sides' differing last lines lack newlines, the raw unified_diff output 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.py assert exact patch text for the context-line and changed-line cases.

`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.
@Liam-DeVoe Liam-DeVoe changed the title claude: emit "\ No newline at end of file" markers in make_patch Fix not emitting newline markers in patches May 26, 2026
@Liam-DeVoe Liam-DeVoe enabled auto-merge May 26, 2026 23:46
@Liam-DeVoe Liam-DeVoe merged commit 2ad0ab4 into HypothesisWorks:master May 27, 2026
151 of 153 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pytest plugin produces corrupt patch if source file doesn't end in a newline

2 participants