<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom"><title>Simon Willison's Weblog: monkey-patching</title><link href="http://feeds.simonwillison.net/" rel="alternate"/><link href="http://feeds.simonwillison.net/tags/monkey-patching.atom" rel="self"/><id>http://feeds.simonwillison.net/</id><updated>2026-08-31T23:59:36+00:00</updated><author><name>Simon Willison</name></author><entry><title>Introducing wrapture</title><link href="https://simonwillison.net/2026/Aug/31/introducing-wrapture/" rel="alternate"/><published>2026-08-31T23:59:36+00:00</published><updated>2026-08-31T23:59:36+00:00</updated><id>https://simonwillison.net/2026/Aug/31/introducing-wrapture/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://grahamdumpleton.me/posts/2026/08/introducing-wrapture/"&gt;Introducing wrapture&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
New from Graham Dumpleton (of &lt;a href="https://pypi.org/project/wrapt/"&gt;wrapt&lt;/a&gt;, mod_wsgi, and New Relic's Python agent fame), who describes Wrapture as taking the monkeypatching ideas from wrapt and extending them to apply to testing and tracing at the same time.&lt;/p&gt;
&lt;p&gt;Wrapture (&lt;a href="https://wrapture.readthedocs.io/"&gt;full documentation here&lt;/a&gt;) makes it easy to wrap any function or method such that all access can be traced, or can be overridden to return a different value.&lt;/p&gt;
&lt;p&gt;It acts as both an alternative to &lt;code&gt;unittest.mock&lt;/code&gt; and a way to implement tracing against an existing project:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Attaching observation to code you do not control, recording what flows through it, and doing so without disturbing the program being watched, is a problem I have never really stopped thinking about.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Wrapture includes &lt;a href="https://wrapture.readthedocs.io/en/latest/otel-export.html"&gt;OpenTelemetry support&lt;/a&gt; and even has an entirely configuration-based mechanism for adding tracing to an existing Python project, which looks like this:&lt;/p&gt;
&lt;div class="highlight highlight-source-toml"&gt;&lt;pre&gt;&lt;span class="pl-smi"&gt;capture&lt;/span&gt; = &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;summary&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;

[[&lt;span class="pl-en"&gt;observe&lt;/span&gt;]]
&lt;span class="pl-smi"&gt;target&lt;/span&gt; = &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;domain:Calculator&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;
&lt;span class="pl-smi"&gt;name&lt;/span&gt; = [&lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;outer&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;inner&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;]

[[&lt;span class="pl-en"&gt;sink&lt;/span&gt;]]
&lt;span class="pl-smi"&gt;type&lt;/span&gt; = &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;jsonlines&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;
&lt;span class="pl-smi"&gt;path&lt;/span&gt; = &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;trace.jsonl&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is still a very young project - just a few weeks old - but it's off to a very promising start.&lt;/p&gt;
&lt;p&gt;Interestingly, this is also Graham's first attempt at  large entirely agent-driven project:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Every line of code and documentation in wrapture was written by an AI assistant working under my direction. I want to be upfront about that, and equally upfront about what it was not. This was not vibe coding, where a one-shot prompt produces a pile of generated code and the person driving hopes for the best because they lack the knowledge to judge what came back. Vibe coding has earned its bad reputation. I engineered wrapture carefully from the start. I have spent a long time in this particular corner of Python and knew exactly what the result needed to be, and the AI was the means of producing it rather than the source of the design.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In a follow-up post, &lt;a href="https://grahamdumpleton.me/posts/2026/09/unit-testing-with-wrapture/"&gt;Unit testing with wrapture&lt;/a&gt;, Graham shows the testing patterns supported by the new library:&lt;/p&gt;
&lt;pre&gt;&lt;span class="pl-k"&gt;def&lt;/span&gt; &lt;span class="pl-en"&gt;test_stub_with_wrapture&lt;/span&gt;():
    &lt;span class="pl-k"&gt;with&lt;/span&gt; &lt;span class="pl-s1"&gt;wrapture&lt;/span&gt;.&lt;span class="pl-c1"&gt;binding&lt;/span&gt;(
        &lt;span class="pl-v"&gt;Gateway&lt;/span&gt;, &lt;span class="pl-s"&gt;"charge"&lt;/span&gt;
    ).&lt;span class="pl-c1"&gt;on_call&lt;/span&gt;.&lt;span class="pl-c1"&gt;returns&lt;/span&gt;({
        &lt;span class="pl-s"&gt;"id"&lt;/span&gt;: &lt;span class="pl-s"&gt;"stub"&lt;/span&gt;, &lt;span class="pl-s"&gt;"amount"&lt;/span&gt;: &lt;span class="pl-c1"&gt;0&lt;/span&gt;}
    ):
        &lt;span class="pl-k"&gt;assert&lt;/span&gt; &lt;span class="pl-en"&gt;OrderService&lt;/span&gt;().&lt;span class="pl-c1"&gt;place&lt;/span&gt;(
            &lt;span class="pl-c1"&gt;500&lt;/span&gt;
        )[&lt;span class="pl-s"&gt;"id"&lt;/span&gt;] &lt;span class="pl-c1"&gt;==&lt;/span&gt; &lt;span class="pl-s"&gt;"stub"&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And this neat example of a test that calls and then modifies the return value from the original method:&lt;/p&gt;
&lt;pre&gt;&lt;span class="pl-k"&gt;def&lt;/span&gt; &lt;span class="pl-en"&gt;test_pinned_result_with_wrapture&lt;/span&gt;():
    &lt;span class="pl-s1"&gt;charge&lt;/span&gt; &lt;span class="pl-c1"&gt;=&lt;/span&gt; &lt;span class="pl-s1"&gt;wrapture&lt;/span&gt;.&lt;span class="pl-c1"&gt;binding&lt;/span&gt;(
        &lt;span class="pl-v"&gt;Gateway&lt;/span&gt;, &lt;span class="pl-s"&gt;"charge"&lt;/span&gt;
    )
    &lt;span class="pl-s1"&gt;charge&lt;/span&gt;.&lt;span class="pl-c1"&gt;on_call&lt;/span&gt;.&lt;span class="pl-c1"&gt;transforms_result&lt;/span&gt;(
        &lt;span class="pl-k"&gt;lambda&lt;/span&gt; &lt;span class="pl-s1"&gt;r&lt;/span&gt;: {&lt;span class="pl-c1"&gt;**&lt;/span&gt;&lt;span class="pl-s1"&gt;r&lt;/span&gt;, &lt;span class="pl-s"&gt;"id"&lt;/span&gt;: &lt;span class="pl-s"&gt;"ch_TEST"&lt;/span&gt;}
    )
    &lt;span class="pl-k"&gt;with&lt;/span&gt; &lt;span class="pl-s1"&gt;charge&lt;/span&gt;:
        &lt;span class="pl-k"&gt;assert&lt;/span&gt; &lt;span class="pl-en"&gt;OrderService&lt;/span&gt;().&lt;span class="pl-c1"&gt;place&lt;/span&gt;(
           &lt;span class="pl-c1"&gt;500&lt;/span&gt;
        ) &lt;span class="pl-c1"&gt;==&lt;/span&gt; {
            &lt;span class="pl-s"&gt;"id"&lt;/span&gt;: &lt;span class="pl-s"&gt;"ch_TEST"&lt;/span&gt;, &lt;span class="pl-s"&gt;"amount"&lt;/span&gt;: &lt;span class="pl-c1"&gt;500&lt;/span&gt;
        }&lt;/pre&gt;

&lt;p&gt;(In both of these examples the &lt;code&gt;OrderService().place(...)&lt;/code&gt; method calls &lt;code&gt;Gateway().charge(...)&lt;/code&gt;.)


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/graham-dumpleton"&gt;graham-dumpleton&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/monkey-patching"&gt;monkey-patching&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/testing"&gt;testing&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/pytest"&gt;pytest&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/observability"&gt;observability&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ai-assisted-programming"&gt;ai-assisted-programming&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/agentic-engineering"&gt;agentic-engineering&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/opentelemetry"&gt;opentelemetry&lt;/a&gt;&lt;/p&gt;



</summary><category term="graham-dumpleton"/><category term="monkey-patching"/><category term="python"/><category term="testing"/><category term="pytest"/><category term="observability"/><category term="ai-assisted-programming"/><category term="agentic-engineering"/><category term="opentelemetry"/></entry><entry><title>s3monkey: A Python library that allows you to interact with Amazon S3 Buckets as if they are your local filesystem.</title><link href="https://simonwillison.net/2018/Feb/21/s3monkey/" rel="alternate"/><published>2018-02-21T17:54:36+00:00</published><updated>2018-02-21T17:54:36+00:00</updated><id>https://simonwillison.net/2018/Feb/21/s3monkey/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/kennethreitz/s3monkey"&gt;s3monkey: A Python library that allows you to interact with Amazon S3 Buckets as if they are your local filesystem.&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
A particularly devious hack by Kenneth Reitz—provides a context manager within which various Python filesystem APIs such as open() and os.listdir() are monkeypatched to operate against an S3 bucket instead. Kenneth built it to make it easier to work with files from apps running on Heroku. Under the hood it uses pyfakefs, a filesystem mocking library originally released by Google.

    &lt;p&gt;&lt;small&gt;&lt;/small&gt;Via &lt;a href="https://twitter.com/kennethreitz/status/966364124232941569"&gt;@kennethreitz&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/monkey-patching"&gt;monkey-patching&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/s3"&gt;s3&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/heroku"&gt;heroku&lt;/a&gt;&lt;/p&gt;



</summary><category term="monkey-patching"/><category term="python"/><category term="s3"/><category term="heroku"/></entry><entry><title>Appending the request URL to SQL statements in Django</title><link href="https://simonwillison.net/2010/Jun/2/framewalking/" rel="alternate"/><published>2010-06-02T09:09:00+00:00</published><updated>2010-06-02T09:09:00+00:00</updated><id>https://simonwillison.net/2010/Jun/2/framewalking/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://chris-lamb.co.uk/2010/06/01/appending-request-url-sql-statements-django/"&gt;Appending the request URL to SQL statements in Django&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
A clever frame-walking monkey-patch which pulls the most recent HttpRequest object out of the Python stack and adds the current request.path to each SQL query as an SQL comment, so you can see it in debugging tools such as slow query logs and the PostgreSQL “select * from pg_stat_activity” query.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/chris-lamb"&gt;chris-lamb&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/debugging"&gt;debugging&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/django"&gt;django&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/monkey-patching"&gt;monkey-patching&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/orm"&gt;orm&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/postgresql"&gt;postgresql&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/sql"&gt;sql&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/recovered"&gt;recovered&lt;/a&gt;&lt;/p&gt;



</summary><category term="chris-lamb"/><category term="debugging"/><category term="django"/><category term="monkey-patching"/><category term="orm"/><category term="postgresql"/><category term="python"/><category term="sql"/><category term="recovered"/></entry><entry><title>What's wrong with extending the DOM</title><link href="https://simonwillison.net/2010/Apr/11/extending/" rel="alternate"/><published>2010-04-11T22:03:39+00:00</published><updated>2010-04-11T22:03:39+00:00</updated><id>https://simonwillison.net/2010/Apr/11/extending/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://perfectionkills.com/whats-wrong-with-extending-the-dom/"&gt;What&amp;#x27;s wrong with extending the DOM&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Detailed explanation of the problems that crop up from extending built-in DOM objects using JavaScript, from Prototype developer kangax. Prototype 2.0 will be dropping this technique entirely—will MooTools follow suit?


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/kangax"&gt;kangax&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/monkey-patching"&gt;monkey-patching&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/mootools"&gt;mootools&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/prototype-js"&gt;prototype-js&lt;/a&gt;&lt;/p&gt;



</summary><category term="javascript"/><category term="kangax"/><category term="monkey-patching"/><category term="mootools"/><category term="prototype-js"/></entry><entry><title>Quoting Adam Kennedy</title><link href="https://simonwillison.net/2008/Mar/22/octarine/" rel="alternate"/><published>2008-03-22T00:28:50+00:00</published><updated>2008-03-22T00:28:50+00:00</updated><id>https://simonwillison.net/2008/Mar/22/octarine/</id><summary type="html">
    &lt;blockquote cite="http://avdi.org/devblog/2008/02/23/why-monkeypatching-is-destroying-ruby/#comment-11"&gt;&lt;p&gt;The Perl community has a long-standing love/hate-affair with making changes that impose "spooky action at a distance". They call it "black magic" and it is generally considered it a last resort. Black Magic that makes GLOBAL changes to things like inheritance is often characterised as being "Octarine" (see disk world novels), because it tends to work ok when there's only one person doing it, but start to mix a few together and KABOOM!&lt;/p&gt;&lt;/blockquote&gt;
&lt;p class="cite"&gt;&amp;mdash; &lt;a href="http://avdi.org/devblog/2008/02/23/why-monkeypatching-is-destroying-ruby/#comment-11"&gt;Adam Kennedy&lt;/a&gt;&lt;/p&gt;

    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/adam-kennedy"&gt;adam-kennedy&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/blackmagic"&gt;blackmagic&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/magic"&gt;magic&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/monkey-patching"&gt;monkey-patching&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/perl"&gt;perl&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ruby"&gt;ruby&lt;/a&gt;&lt;/p&gt;



</summary><category term="adam-kennedy"/><category term="blackmagic"/><category term="magic"/><category term="monkey-patching"/><category term="perl"/><category term="ruby"/></entry><entry><title>Monkeypatching is Destroying Ruby</title><link href="https://simonwillison.net/2008/Mar/22/virtuous/" rel="alternate"/><published>2008-03-22T00:27:02+00:00</published><updated>2008-03-22T00:27:02+00:00</updated><id>https://simonwillison.net/2008/Mar/22/virtuous/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://avdi.org/devblog/2008/02/23/why-monkeypatching-is-destroying-ruby/"&gt;Monkeypatching is Destroying Ruby&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Deliberately provocative title, but makes a well considered case for restrained use of monkey patching in Ruby. Cultural norms around monkey patching seem to me to be one of the core differences between the Ruby and Python communities.

    &lt;p&gt;&lt;small&gt;&lt;/small&gt;Via &lt;a href="http://blog.ianbicking.org/2008/03/21/monkeypatching-and-dead-ends/"&gt;Ian Bicking&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/ian-bicking"&gt;ian-bicking&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/monkey-patching"&gt;monkey-patching&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ruby"&gt;ruby&lt;/a&gt;&lt;/p&gt;



</summary><category term="ian-bicking"/><category term="monkey-patching"/><category term="python"/><category term="ruby"/></entry><entry><title>Hacking Contributed Models</title><link href="https://simonwillison.net/2008/Mar/11/djangotricks/" rel="alternate"/><published>2008-03-11T05:51:00+00:00</published><updated>2008-03-11T05:51:00+00:00</updated><id>https://simonwillison.net/2008/Mar/11/djangotricks/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://djangotricks.blogspot.com/2008/03/hacking-contributed-models.html"&gt;Hacking Contributed Models&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Neat Django trick using monkeypatching to make some minor tweaks to built-in contributed models such as auth or flatpages.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/contrib"&gt;contrib&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/django"&gt;django&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/monkey-patching"&gt;monkey-patching&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;&lt;/p&gt;



</summary><category term="contrib"/><category term="django"/><category term="monkey-patching"/><category term="python"/></entry><entry><title>Monkeypatching idioms - elegant or ugly?</title><link href="https://simonwillison.net/2008/Jan/30/monkeypatching/" rel="alternate"/><published>2008-01-30T00:39:36+00:00</published><updated>2008-01-30T00:39:36+00:00</updated><id>https://simonwillison.net/2008/Jan/30/monkeypatching/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://mail.python.org/pipermail/python-dev/2008-January/076194.html"&gt;Monkeypatching idioms - elegant or ugly?&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Guido offers a decorator and a metaclass as syntactic sugar for monkeypatching existing Python classes.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/decorators"&gt;decorators&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/guido-van-rossum"&gt;guido-van-rossum&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/idioms"&gt;idioms&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/metaclasses"&gt;metaclasses&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/monkey-patching"&gt;monkey-patching&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;&lt;/p&gt;



</summary><category term="decorators"/><category term="guido-van-rossum"/><category term="idioms"/><category term="metaclasses"/><category term="monkey-patching"/><category term="python"/></entry><entry><title>Test stubbing httplib2</title><link href="https://simonwillison.net/2007/May/10/joe/" rel="alternate"/><published>2007-05-10T23:24:09+00:00</published><updated>2007-05-10T23:24:09+00:00</updated><id>https://simonwillison.net/2007/May/10/joe/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://bitworking.org/news/172/Test-stubbing-httplib2"&gt;Test stubbing httplib2&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Nice demonstration of monkey-patching as part of unit testing in Python.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/httplib2"&gt;httplib2&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/joe-gregorio"&gt;joe-gregorio&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/monkey-patching"&gt;monkey-patching&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/testing"&gt;testing&lt;/a&gt;&lt;/p&gt;



</summary><category term="httplib2"/><category term="joe-gregorio"/><category term="monkey-patching"/><category term="python"/><category term="testing"/></entry></feed>