Registry / testing / patchy

patchy

JSON →
library3.1.0pypypi✓ verified 21d ago

Patchy is a Python library that allows patching the inner source code of Python functions at runtime. Unlike traditional monkey patching, which replaces function objects, Patchy modifies the function's `__code__` attribute, ensuring that all references to the function (even those imported elsewhere) reflect the new behavior. It achieves this by using the standard `patch` command-line utility to apply diffs to the function's source code. The current version is 2.10.0, and it generally follows a release cadence tied to bug fixes and feature enhancements, compatible with Python 3.9 to 3.14.

pip install patchy
INSTALL
IMPORT
SIG · PATCHY
P
patchy
testingpythonv3.1.0
Install
1.6s avg
Import
46ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.046s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.046s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

patch
from patchy import patch
import patchy

This example demonstrates how to use `patchy.patch()` to modify the runtime behavior of a Python function by applying a standard diff format string. The `patch` function takes the target function (or its dotted path as a string) and the patch text.

import patchy def sample(): return 1 print(f"Original output: {sample()}") # Apply a patch to change the function's behavior patchy.patch( sample, """\ def sample(): - return 1 + return 9001 """, ) print(f"Patched output: {sample()}")
patchy --version
Debug
Known issues
gotchaPatches are applied against the function's source code string. Any change to the original function, even adding a comment or reformatting, can cause the patch to fail due to context lines no longer matching. This will raise a `ValueError`.
fix
Ensure patches are as minimal as possible and carefully test against library updates. Consider using version control to manage patches and their corresponding library versions.
affects: All versions
gotchaThe `patch_text` argument needs careful formatting. It must be a triple-quoted string that starts with a backslash (e.g., `"""\ ...""")`) to correctly `textwrap.dedent()` the patch content without removing leading whitespace that is part of the patch.
fix
Always start your `patch_text` triple-quoted string with a backslash immediately after the opening quotes, followed by a newline, to ensure correct indentation handling.
affects: All versions
gotchaPatchy works by writing the function's source to a temporary file, calling the external `patch` command-line utility, recompiling the new source, and replacing the function's code object. This process can be relatively slow and introduces a dependency on an external system utility.
fix
Use Patchy for targeted, infrequent runtime modifications rather than highly performance-critical operations. Ensure the `patch` command-line utility is available in the execution environment.
affects: All versions
gotchaPatchy is designed for patching the *source of python functions*. It does not support broader patching operations like file renaming, creation, removal, or complex directory tree operations, which some other patching tools might handle. Its scope is specifically limited to modifying existing function bodies.
fix
Understand Patchy's specific use case (function source patching) and choose other tools for broader file or repository-level patching requirements.
affects: All versions
Upgrade
Version history
3.1.0latest on PyPI · released Aug 21, 2026
Audit
Dependencies
patchrequiredPatchy relies on the system's 'patch' command-line utility to apply source code diffs. This is an external system dependency, not a Python package.
Agent activity
9 hits · last 30 days
node
6
Resources