Types-Unidiff
`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
- breaking Stub package versions align with the runtime library (e.g., `types-unidiff==0.7.*` targets `unidiff==0.7.*`). However, any version bump in the stub package can introduce changes that might cause your code to fail type checks, even if the runtime library itself hasn't changed.
- gotcha `types-unidiff` is solely for type checking. It provides no runtime functionality. The actual `unidiff` library must be installed alongside `types-unidiff` for your code to execute correctly.
- gotcha Do not attempt to import symbols directly from `types_unidiff` (e.g., `from types_unidiff import PatchSet`). Type stubs are consumed by type checkers but are not designed for direct runtime imports. Always import classes and functions from the original `unidiff` package.
Install
-
pip install types-unidiff unidiff
Imports
- PatchSet
from unidiff import PatchSet
- PatchedFile
from unidiff import PatchedFile
- Hunk
from unidiff import Hunk
Quickstart
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()}")