patchy
Patch the inner source of python functions at runtime.
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 on this page — 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
patchy on PyPI
pip
pip install patchyuv
uv add patchypoetry
poetry add patchyInstalling 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 the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 339 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 894,302/month — #4,792 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: patchy-2.10.0-py3-none-any.whl
Keywords: patchy
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
patchParses and applies unified diff patches to…
permissive · top 15,000 on PyPI
patch-ngParses and applies unified diff patches to…
permissive · top 5,000 on PyPI
moreorlessGenerates unified diffs with correct handling…
permissive · top 5,000 on PyPI
autowraptAutowrapt enables monkey patching of Python…
permissive · top 15,000 on PyPI
forbiddenfruitForbiddenfruit allows you to dynamically add…
unclear · top 5,000 on PyPI
diff-match-patchProvides diff, match, and patch algorithms for…
permissive · top 5,000 on PyPI
jsonpatchApplies JSON Patches according to RFC 6902,…
permissive · top 1,000 on PyPI
dictdifferDictdiffer computes and applies diffs and…
permissive · top 5,000 on PyPI
django-stubs-extProvides runtime monkey-patching extensions for…
permissive · top 5,000 on PyPI
varnameRetrieves variable names at runtime from inside…
permissive · top 15,000 on PyPI