Types-Unidiff

0.7.0.20260408 · active · verified Mon Apr 13

`types-unidiff` is a PEP 561 type stub package providing external type annotations for the `unidiff` library. `unidiff` itself is a Python library for parsing and interacting with unified diff data, allowing programmatic access to file changes, hunks, and lines within a diff. `types-unidiff` is released frequently (up to daily) by the Typeshed project and is designed to be used by static type checkers like MyPy, Pyright, and PyCharm to ensure type correctness in code that uses `unidiff`.

Warnings

Install

Imports

Quickstart

This example demonstrates parsing a unified diff string using the `unidiff.PatchSet` class. After `types-unidiff` is installed, a static type checker would be able to provide type hints and check for correctness when interacting with `PatchSet`, `PatchedFile`, and `Hunk` objects.

import urllib.request
from unidiff import PatchSet

# Example unified diff content
diff_content = """
diff --git a/file1.py b/file1.py
index abcde12..fghij34 100644
--- a/file1.py
+++ b/file1.py
@@ -1,3 +1,4 @@
-line 1
+new line 1
 line 2
 line 3
+added line 4
"""

# Parse the diff string
patch_set = PatchSet(diff_content)

# Iterate through patched files
for patched_file in patch_set:
    print(f"File: {patched_file.path}")
    print(f"Added lines: {patched_file.added}, Removed lines: {patched_file.removed}")
    for hunk in patched_file:
        print(f"  Hunk header: {hunk.header}")
        for line in hunk:
            print(f"    {line.line_type} {line.value.strip()}")

view raw JSON →