{"id":5002,"library":"patchy","title":"Patchy","description":"Patchy is a Python library that allows patching the inner source code of Python functions at runtime. Unlike traditional monkey patching, which replaces function objects, Patchy modifies the function's `__code__` attribute, ensuring that all references to the function (even those imported elsewhere) reflect the new behavior. It achieves this by using the standard `patch` command-line utility to apply diffs to the function's source code. The current version is 2.10.0, and it generally follows a release cadence tied to bug fixes and feature enhancements, compatible with Python 3.9 to 3.14.","status":"active","version":"2.10.0","language":"en","source_language":"en","source_url":"https://github.com/adamchainz/patchy","tags":["patching","runtime modification","monkey patching","code manipulation"],"install":[{"cmd":"pip install patchy","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Patchy relies on the system's 'patch' command-line utility to apply source code diffs. This is an external system dependency, not a Python package.","package":"patch","optional":false}],"imports":[{"note":"The primary function is `patchy.patch()`, used to apply a diff string to a target function or a dotted path to a function.","symbol":"patch","correct":"import patchy\n\ndef my_func():\n    return 1\n\npatchy.patch(my_func, \"\"\"\\\ndef my_func():\n-    return 1\n+    return 9001\n\"\"\")"}],"quickstart":{"code":"import patchy\n\ndef sample():\n    return 1\n\nprint(f\"Original output: {sample()}\")\n\n# Apply a patch to change the function's behavior\npatchy.patch(\n    sample,\n    \"\"\"\\\ndef sample():\n-    return 1\n+    return 9001\n\"\"\",\n)\n\nprint(f\"Patched output: {sample()}\")","lang":"python","description":"This example demonstrates how to use `patchy.patch()` to modify the runtime behavior of a Python function by applying a standard diff format string. The `patch` function takes the target function (or its dotted path as a string) and the patch text."},"warnings":[{"fix":"Ensure patches are as minimal as possible and carefully test against library updates. Consider using version control to manage patches and their corresponding library versions.","message":"Patches are applied against the function's source code string. Any change to the original function, even adding a comment or reformatting, can cause the patch to fail due to context lines no longer matching. This will raise a `ValueError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always start your `patch_text` triple-quoted string with a backslash immediately after the opening quotes, followed by a newline, to ensure correct indentation handling.","message":"The `patch_text` argument needs careful formatting. It must be a triple-quoted string that starts with a backslash (e.g., `\"\"\"\\\n...\"\"\")`) to correctly `textwrap.dedent()` the patch content without removing leading whitespace that is part of the patch.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use Patchy for targeted, infrequent runtime modifications rather than highly performance-critical operations. Ensure the `patch` command-line utility is available in the execution environment.","message":"Patchy works by writing the function's source to a temporary file, calling the external `patch` command-line utility, recompiling the new source, and replacing the function's code object. This process can be relatively slow and introduces a dependency on an external system utility.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Understand Patchy's specific use case (function source patching) and choose other tools for broader file or repository-level patching requirements.","message":"Patchy is designed for patching the *source of python functions*. It does not support broader patching operations like file renaming, creation, removal, or complex directory tree operations, which some other patching tools might handle. Its scope is specifically limited to modifying existing function bodies.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}