{"id":5541,"library":"types-unidiff","title":"Types-Unidiff","description":"`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`.","status":"active","version":"0.7.0.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed/tree/main/stubs/unidiff","tags":["typing","stubs","diff","typeshed","static analysis"],"install":[{"cmd":"pip install types-unidiff unidiff","lang":"bash","label":"Install `types-unidiff` and `unidiff`"}],"dependencies":[{"reason":"Provides the runtime functionality for which these type stubs are generated.","package":"unidiff","optional":false}],"imports":[{"note":"Type stubs are not meant for direct runtime imports; they are consumed by type checkers. The actual `PatchSet` class is imported from the `unidiff` package.","wrong":"from types_unidiff import PatchSet","symbol":"PatchSet","correct":"from unidiff import PatchSet"},{"note":"Import `PatchedFile` directly from the `unidiff` package.","symbol":"PatchedFile","correct":"from unidiff import PatchedFile"},{"note":"Import `Hunk` directly from the `unidiff` package.","symbol":"Hunk","correct":"from unidiff import Hunk"}],"quickstart":{"code":"import urllib.request\nfrom unidiff import PatchSet\n\n# Example unified diff content\ndiff_content = \"\"\"\ndiff --git a/file1.py b/file1.py\nindex abcde12..fghij34 100644\n--- a/file1.py\n+++ b/file1.py\n@@ -1,3 +1,4 @@\n-line 1\n+new line 1\n line 2\n line 3\n+added line 4\n\"\"\"\n\n# Parse the diff string\npatch_set = PatchSet(diff_content)\n\n# Iterate through patched files\nfor patched_file in patch_set:\n    print(f\"File: {patched_file.path}\")\n    print(f\"Added lines: {patched_file.added}, Removed lines: {patched_file.removed}\")\n    for hunk in patched_file:\n        print(f\"  Hunk header: {hunk.header}\")\n        for line in hunk:\n            print(f\"    {line.line_type} {line.value.strip()}\")","lang":"python","description":"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."},"warnings":[{"fix":"Pin `types-unidiff` to a specific version or a narrow version range (e.g., `types-unidiff~=0.7.0.20260408`) if strict type checking is critical and unexpected type-checking errors are undesirable. Regularly review changes in new stub versions against your codebase.","message":"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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Always install both `types-unidiff` and `unidiff` using `pip install types-unidiff unidiff`.","message":"`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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure all imports refer to the runtime package, e.g., `from unidiff import PatchSet`.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}