Registry / data / unidiff

unidiff

JSON →
library1.0.0pypypi✓ verified 26d ago

unidiff is a simple Python library for parsing and interacting with unified diff data. It allows developers to extract metadata, file changes, and hunk information from diffs. The current version is 0.7.5, and it maintains an active development status with periodic releases addressing bug fixes and feature enhancements.

pip install unidiff
INSTALL
IMPORT
SIG · UNIDIFF
U
unidiff
datapythonv1.0.0
Install
1.6s avg
Import
17ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.018s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.016s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PatchSet
from unidiff import PatchSet

This quickstart demonstrates how to fetch a diff from a URL and parse it using `PatchSet`. It then iterates through the patched files and their hunks to display added and removed lines. `PatchSet` can also be instantiated from a file-like object or directly from a string.

import urllib.request from unidiff import PatchSet # Example: Parse a diff from a GitHub pull request URL diff_url = 'https://github.com/matiasb/python-unidiff/pull/3.diff' with urllib.request.urlopen(diff_url) as response: diff_data = response.read() encoding = response.headers.get_content_charset() or 'utf-8' patch = PatchSet(diff_data.decode(encoding)) print(f"Parsed {len(patch)} files in the diff.") for patched_file in patch: print(f"File: {patched_file.path}") print(f" Added: {patched_file.added} lines, Removed: {patched_file.removed} lines") for hunk in patched_file: for line in hunk: if line.is_added: print(f" + {line.value.strip()}") elif line.is_removed: print(f" - {line.value.strip()}")
Debug
Known issues
breakingIn unidiff v0.7.0, the way renamed files report their target path changed. `PatchedFile.path` now returns the *target* filename for renamed files. If your code previously relied on another attribute or inferred the target path differently, it might break.
fix
Update your code to use `PatchedFile.path` for the target filename of renamed files. Review `PatchedFile` attributes for source filename if needed.
affects: <0.7.0
gotchaOlder versions of unidiff (prior to v0.7.4-0.7.5) had issues parsing git diffs with spaces in filenames or those generated with `--no-prefix`. This could lead to incorrect parsing or errors.
fix
Upgrade to unidiff v0.7.5 or later to ensure correct parsing of diffs with spaces in filenames and `--no-prefix` headers.
affects: <0.7.5
gotchaunidiff might throw `UnidiffParseError: Unexpected hunk found` if the diff input lacks proper filename headers (e.g., `--- a/path/to/file` and `+++ b/path/to/file`). While `difflib.unified_diff()` can produce diffs without these headers, unidiff expects them.
fix
Ensure that the diff data provided to `PatchSet` includes valid filename headers (e.g., `--- a/old_file.py` and `+++ b/new_file.py`). If processing output from `difflib`, you may need to pre-process it to add these headers or handle the `UnidiffParseError`.
affects: All versions
gotchaHandling of binary files was improved in v0.6.0 and v0.7.5. Older versions might not correctly identify or process changes in binary files, or might error if a binary file is the first change in a patch.
fix
Upgrade to unidiff v0.7.5 to ensure robust handling and identification of binary files within patch sets. Use `PatchedFile.is_binary_file` to check file type.
affects: <0.7.5
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'unidiff'
The `unidiff` package has not been installed in the current Python environment.
fix
pip install unidiff
TypeError: expected string or bytes-like object, not _io.TextIOWrapper
The `unidiff.PatchSet` constructor expects the diff content as a string or bytes-like object, but a file object was passed directly.
fix
```python
import unidiff

with open('my_diff.diff', 'r', encoding='utf-8') as f:
    diff_content = f.read() # Read the content into a string
diff_set = unidiff.PatchSet(diff_content)
```
AttributeError: 'PatchSet' object has no attribute 'files'
The `unidiff.PatchSet` object stores individual file changes in its `diffs` attribute (a list of `Diff` objects), not a `files` attribute.
fix
```python
import unidiff

diff_text = """---
a/file.py\n+++ b/file.py\n@@ -1 +1 @@\n-old\n+new\n"""
patch_set = unidiff.PatchSet(diff_text)
for diff_obj in patch_set.diffs:
    print(f"Processing diff for {diff_obj.source_file} -> {diff_obj.target_file}")
```
AttributeError: 'PatchSet' object has no attribute 'is_new_file'
Methods like `is_new_file` and `is_deleted_file` are available on individual `Diff` objects (which represent a single file change), not on the `PatchSet` object which is a collection of diffs.
fix
```python
import unidiff

diff_text = """---
/dev/null\n+++ b/new_file.txt\n@@ -0,0 +1,1 @@\n+New content\n"""
patch_set = unidiff.PatchSet(diff_text)
for diff_obj in patch_set.diffs:
    if diff_obj.is_new_file:
        print(f"'{diff_obj.target_file}' is a new file.")
```
Upgrade
Version history
1.0.0latest on PyPI · released Jul 25, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources
unidiff — pip install unidiff · libregistry