patchy
Patch the inner source of python functions at runtime.
Decision gist · record as of 2026-08-14
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.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- The system `patch` command-line utility must be available on the system for patchy to function.
- 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 · maintenance · safety
MIT (permissive) — Licensed under MIT, a permissive license that allows free use, modification, and distribution with minimal restrictions.
last release 2025-09-09 (339 days) · last repo commit 2026-08-13 · 216 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 894,302 downloads/mo, #4,792 on PyPI
Alternatives
Verify before relying
import patchy
def sample():
return 1
patchy.patch(
sample,
"""\
@@ -1,2 +1,2 @@
def sample():
- return 1
+ return 9001
""",
)
print(sample()) # prints 9001- 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
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 on it.
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
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.
The system `patch` command-line utility must be available on the system for patchy to function.
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
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 |
| Classifiers | Development Status :: 5 - Production/StableIntended Audience :: DevelopersNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.9Typing :: Typed |
Evidence: patchy-2.10.0-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “runtime function patching”
- patchyPatchy modifies Python functions at runtime by replacing their code…
- codefindCodefind locates code objects by filename and qualified name,…
- wraptwrapt provides a Python module for building decorators, object…
Give your agent the search over MCP, or paste the wish link into any chat.
More Software Development packages
Provides backported and experimental type hints for Python 3.9+, allowing use of newer typing features on older Python versions and enabling early experimentation with type system PEPs before they enter the standard library.
NumPy provides an N-dimensional array object and a comprehensive suite of mathematical, linear algebra, Fourier transform, and random number functions for scientific computing in Python.
FastAPI is a Python web framework for building REST APIs using type hints, with automatic request validation, serialization, and interactive API documentation.
Provides a way to document function parameters, class attributes, return types, and variables inline using Python's `Annotated` type hint syntax instead of traditional docstrings.
Typer builds command-line applications from Python functions using type hints, automatically generating help text, argument parsing, and shell completion.
Install it if you are building CLIs in Python.
Distlib provides low-level packaging utilities for building, distributing, and managing Python software—including metadata handling, version specifiers, wheel support, script installation, and dependency resolution.
See also patch · patch-ng · moreorless · autowrapt · forbiddenfruit · diff-match-patch · jsonpatch · dictdiffer · django-stubs-ext · varname