--- id: patchy version: "2.10.0" license: MIT license_treatment: permissive maintenance: active --- # patchy — Patch the inner source of python functions at runtime. License: permissive · Maintenance: active · Downloads: 894.3K/mo ## What it is and what it does Patchy lets you modify the behavior of Python functions at runtime by applying unified diff patches to their source code. Instead of monkey-patching or forking a library, you specify exactly what lines should change using standard patch format, and patchy rewrites the function's code object. This is useful when you need to apply small, targeted fixes to external libraries without maintaining a full fork—the patch includes context lines that must match, so you'll catch if the underlying function has changed unexpectedly. The package works by extracting the function's source with `inspect.getsource()`, applying the patch via the system `patch` utility, recompiling the modified code, and replacing the function's code object. It handles special cases like instance methods, class methods, and static methods transparently. It also provides a context manager and decorator (`temp_patch`) for temporary patches, and a simpler `replace()` function for cases where you'd rather provide the expected and new source directly instead of writing a diff. Use it for: - Apply targeted fixes to external library functions without maintaining a fork or monkey patch - Temporarily modify function behavior in tests using the context manager or decorator form - Replace a function's implementation with a simpler alternative using the `replace()` API - Verify that a function hasn't changed before applying a patch, catching unexpected upstream modifications - Patch methods on classes you don't own, with context validation to detect breaking changes ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Patchy modifies Python functions at runtime by replacing their code objects with patched versions, using standard unified diff format to specify changes while keeping the function object itself intact. Yes, if you need to apply small, context-validated patches to functions in external libraries and want better visibility than monkey-patching. The low install friction, active maintenance, no runtime dependencies, and MIT license make it safe to add. However, consider whether a fork, a pull request to the upstream project, or a simpler monkey patch would be more maintainable for your use case—patchy is best for temporary or niche fixes, not as a substitute for proper dependency management. ## Install pip install patchy uv add patchy poetry add patchy ## Installing patchy Before you install: Installation is straightforward with no runtime dependencies and low friction. The package is actively maintained with recent commits and has been stable since its first release in 2015. License in practice: Licensed under MIT, a permissive license that allows free use, modification, and distribution with minimal restrictions. Quickstart: import patchy def sample(): return 1 patchy.patch( sample, """\ @@ -1,2 +1,2 @@ def sample(): - return 1 + return 9001 """, ) print(sample()) # prints 9001 The system `patch` command-line utility must be available on the system for patchy to function. Verify before relying: - Whether the `patch` command-line utility must be installed on the system for patchy to function - Performance characteristics and overhead of writing source to disk and invoking the patch utility - Compatibility with functions defined in C extensions or compiled code ## Package facts - License: MIT (permissive) - Python support: supports_current - Install friction: low - Maintenance: active - Downloads: 894.3K/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags runtime function patching, modify python function source, apply patches to functions, runtime code replacement, function behavior modification, patch function at runtime, dynamic function patching, runtime-patching, testing-utilities, code-modification [View on SkillFed](https://skillfed.io/packages/patchy) · [View on PyPI](https://pypi.org/project/patchy/)