skillfed

patchy

Patch the inner source of python functions at runtime.

patchy v2.10.0 894.3K downloads/30d#4,792 on PyPI216
Permissive license MIT Active released

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 patchy

uv

uv add patchy

poetry

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 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

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

Tags

runtime function patchingmodify python function sourceapply patches to functionsruntime code replacementfunction behavior modificationpatch function at runtimedynamic function patching
runtime-patchingtesting-utilitiescode-modification

More Software Development packages