{"library":"unidiff","title":"unidiff","description":"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.","status":"active","version":"0.7.5","language":"en","source_language":"en","source_url":"https://github.com/matiasb/python-unidiff","tags":["diff","unified diff","git","parsing","patch"],"install":[{"cmd":"pip install unidiff","lang":"bash","label":"Install stable release"}],"dependencies":[],"imports":[{"symbol":"PatchSet","correct":"from unidiff import PatchSet"}],"quickstart":{"code":"import urllib.request\nfrom unidiff import PatchSet\n\n# Example: Parse a diff from a GitHub pull request URL\ndiff_url = 'https://github.com/matiasb/python-unidiff/pull/3.diff'\nwith urllib.request.urlopen(diff_url) as response:\n    diff_data = response.read()\n    encoding = response.headers.get_content_charset() or 'utf-8'\n    patch = PatchSet(diff_data.decode(encoding))\n\nprint(f\"Parsed {len(patch)} files in the diff.\")\nfor patched_file in patch:\n    print(f\"File: {patched_file.path}\")\n    print(f\"  Added: {patched_file.added} lines, Removed: {patched_file.removed} lines\")\n    for hunk in patched_file:\n        for line in hunk:\n            if line.is_added:\n                print(f\"    + {line.value.strip()}\")\n            elif line.is_removed:\n                print(f\"    - {line.value.strip()}\")","lang":"python","description":"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."},"warnings":[{"fix":"Update your code to use `PatchedFile.path` for the target filename of renamed files. Review `PatchedFile` attributes for source filename if needed.","message":"In 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.","severity":"breaking","affected_versions":"<0.7.0"},{"fix":"Upgrade to unidiff v0.7.5 or later to ensure correct parsing of diffs with spaces in filenames and `--no-prefix` headers.","message":"Older 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.","severity":"gotcha","affected_versions":"<0.7.5"},{"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`.","message":"unidiff 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Handling 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.","severity":"gotcha","affected_versions":"<0.7.5"}],"env_vars":null,"last_verified":"2026-04-05T00:00:00.000Z","next_check":"2026-07-04T00:00:00.000Z"}