Skip to main content
https://img.shields.io/github/actions/workflow/status/adamchainz/patchy/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/patchy.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit
A patchy pirate.

Patch the inner source of python functions at runtime.

A quick example, making a function that returns 1 instead return 9001:

>>> def sample():
...     return 1
...
>>> patchy.patch(
...     sample,
...     """\
...     @@ -1,2 +1,2 @@
...      def sample():
...     -    return 1
...     +    return 9001
...     """,
... )
>>> sample()
9001

Patchy works by replacing the code attribute of the function, leaving the function object itself the same. It’s thus more versatile than monkey patching, since if the function has been imported in multiple places they’ll also call the new behaviour.


Hacking on a Django project? Check out my book Boost Your Django DX which covers many ways to improve your development experience.


Installation

Use pip:

python -m pip install patchy

Python 3.10 to 3.15 supported.

Why?

If you’re monkey-patching an external library to add or fix some functionality, you will probably forget to check the monkey patch when you upgrade it. By using a patch against its source code, you can specify some context that you expect to remain the same in the function that will be checked before the source is applied.

I found this with some small but important patches to Django for a project. Since it takes a lot of energy to maintain a fork, writing monkey patches was the chosen quick solution, but then writing actual patches would be better.

The patches are applied in-memory by unipatch, a pure-Python implementation of unified diff patching, compatible with the behaviour of the GNU patch commandline utility.

Why not?

There are of course a lot of reasons against:

  • If you have a patch file, why not just fork the library and apply it?

  • At least with monkey-patching you know what end up with, rather than having the changes being done at runtime to source that may have changed.

All are valid arguments. However once in a while this might be the right solution.

How?

The standard library function inspect.getsource() is used to retrieve the source code of the function, the patch is applied in-memory with unipatch, the code is recompiled, and the function’s code object is replaced the new one. Because nothing tends to poke around at code objects apart from dodgy hacks like this, you don’t need to worry about chasing any references that may exist to the function, unlike mock.patch.

A little special treatment is given to instancemethod, classmethod, and staticmethod objects to make sure the underlying function is what gets patched and that you don’t have to worry about the details.

API

patch(func, patch_text)

Apply the patch patch_text to the source of function func. func may be either a function, or a string providing the dotted path to import a function.

If the patch is invalid, for example the context lines don’t match, ValueError will be raised, with a message that includes all the output from the patch utility.

Note that patch_text will be textwrap.dedent()’ed, but leading whitespace will not be removed. Therefore the correct way to include the patch is with a triple-quoted string with a backslash - """\ - which starts the string and avoids including the first newline. A final newline is not required and will be automatically added if not present.

Example:

import patchy


def sample():
    return 1


patchy.patch(
    sample,
    """\
    @@ -2,2 +2,2 @@
    -    return 1
    +    return 2""",
)

print(sample())  # prints 2

mc_patchface(func, patch_text)

An alias for patch, so you can meme it up by calling patchy.mc_patchface().

unpatch(func, patch_text)

Unapply the patch patch_text from the source of function func. This is the reverse of patch()ing it, and calls patch --reverse.

The same error and formatting rules apply as in patch().

Example:

import patchy


def sample():
    return 2


patchy.unpatch(
    sample,
    """\
    @@ -2,2 +2,2 @@
    -    return 1
    +    return 2""",
)

print(sample())  # prints 1

temp_patch(func, patch_text)

Takes the same arguments as patch. Usable as a context manager or function decorator to wrap code with a call to patch before and unpatch after.

Context manager example:

def sample():
    return 1234


patch_text = """\
    @@ -1,2 +1,2 @@
     def sample():
    -    return 1234
    +    return 5678
    """

with patchy.temp_patch(sample, patch_text):
    print(sample())  # prints 5678

Decorator example, using the same sample and patch_text:

@patchy.temp_patch(sample, patch_text)
def my_func():
    return sample() == 5678


print(my_func())  # prints True

replace(func, expected_source, new_source)

Check that function or dotted path to function func has an AST matching expected_source, then replace its inner code object with source compiled from new_source. If the AST check fails, ValueError will be raised with current/expected source code in the message. In the author’s opinion it’s preferable to call patch() so your call makes it clear to see what is being changed about func, but using replace() is simpler as you don’t have to make a patch and there is no subprocess call to the patch utility.

Note both expected_source and new_source will be textwrap.dedent()’ed, so the best way to include their source is with a triple quoted string with a backslash escape on the first line, as per the example below.

If you want, you can pass expected_source=None to avoid the guard against your target changing, but this is highly unrecommended as it means if the original function changes, the call to replace() will continue to silently succeed.

Example:

import patchy


def sample():
    return 1


patchy.replace(
    sample,
    """\
    def sample():
        return 1
    """,
    """\
    def sample():
        return 42
    """,
)

print(sample())  # prints 42

How to Create a Patch

  1. Save the source of the function of interest (and nothing else) in a .py file, e.g. before.py:

    def foo():
        print("Change me")

    Make sure you dedent it so there is no whitespace before the def, i.e. d is the first character in the file. For example if you wanted to patch the bar() method below:

    class Foo:
        def bar(self, x):
            return x * 2

    …you would put just the method in a file like so:

    def bar(self, x):
        return x * 2

    However we’ll continue with the first example before.py since it’s simpler.

  2. Copy that .py file, to e.g. after.py, and make the changes you want, such as:

    def foo():
        print("Changed")
  3. Run diff, e.g. diff -u before.py after.py. You will get output like:

    diff --git a/Users/chainz/tmp/before.py b/Users/chainz/tmp/after.py
    index e6b32c6..31fe8d9 100644
    --- a/Users/chainz/tmp/before.py
    +++ b/Users/chainz/tmp/after.py
    @@ -1,2 +1,2 @@
     def foo():
    -    print("Change me")
    +    print("Changed")
  4. The filenames are not necessary for patchy to work. Take only from the first @@ line onwards into the multiline string you pass to patchy.patch():

    patchy.patch(
        foo,
        """\
        @@ -1,2 +1,2 @@
         def foo():
        -    print("Change me")
        +    print("Changed")
        """,
    )

Release files for patchy 3.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for patchy 3.1.0
File Size Uploaded
patchy-3.1.0.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for patchy 3.1.0
File Interpreter ABI Platform
patchy-3.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.0 kB

Release files / patchy-3.1.0.tar.gz

Download URL patchy-3.1.0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e75610e820d179ef646bfee4ccb5ae2cbfdaf6940e9946ce7b715dc544aa8e50
BLAKE2b-256 checksum
How to use checksums
6e180ce2ae80d8383f43d091e3d909cc38c547602c33ec537ae8d1aeae37bd10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 21, 2026.

Transparency log

Release files / patchy-3.1.0-py3-none-any.whl

Download URL patchy-3.1.0-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b09087bdb5fa7ed27a2b54a49dd19fd201442bdd01da378970d168b28aefc48
BLAKE2b-256 checksum
How to use checksums
4c03f413a46506bbcf11ebb0d7f36c1d7629ff90b368d13d9b511349a0825c79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 21, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

3.1.0 This release

2 release files

3.0.0

2 release files

2.9.0

2 release files

2.8.0

2 release files

2.7.0

2 release files

2.6.1

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page