{"id":5707,"library":"python-monkey-business","title":"Python Monkey Business","description":"python-monkey-business is a Python package offering utility functions for monkey-patching code at runtime. It primarily provides a decorator to replace functions within classes or modules. The current version is 1.1.0, with the latest package upload on PyPI on July 11, 2024, though the project appears to have a very infrequent release cadence.","status":"maintenance","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/theatlantic/python-monkey-business","tags":["monkey-patching","utility","runtime","decorator"],"install":[{"cmd":"pip install python-monkey-business","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"monkeybiz","correct":"import monkeybiz"},{"note":"Typically used as a decorator: @monkeybiz.patch(...)","symbol":"patch","correct":"from monkeybiz import patch"},{"note":"Used to revert a monkey-patched function to its original state.","symbol":"unpatch","correct":"from monkeybiz import unpatch"}],"quickstart":{"code":"import monkeybiz\n\n# Assume 'foomodule' and 'FooClass' exist for demonstration\nclass FooClass:\n    def bar(self):\n        return \"original\"\n\n# Patch a method in a class\n@monkeybiz.patch(FooClass)\ndef bar(original_fn, *args, **kwargs):\n    print(\"Patched!\")\n    return \"patched_\" + original_fn(*args, **kwargs)\n\ninstance = FooClass()\nprint(instance.bar()) # Should print 'Patched!' and 'patched_original'\n\nmonkeybiz.unpatch(FooClass, 'bar')\nprint(instance.bar()) # Should print 'original' again\n\n# Example of patching a module-level function (requires a dummy module)\n# import sys\n# class DummyModule:\n#     def baz(): return 'original_module_baz'\n# sys.modules['barmodule'] = DummyModule()\n# import barmodule\n#\n# @monkeybiz.patch(barmodule)\n# def baz(original_fn, *args, **kwargs):\n#     return 'patched_' + original_fn(*args, **kwargs)\n#\n# print(barmodule.baz()) # Should print 'patched_original_module_baz'\n# monkeybiz.unpatch(barmodule, 'baz')","lang":"python","description":"Demonstrates how to use the `monkeybiz.patch` decorator to replace a method within a class and how to revert the patch using `monkeybiz.unpatch`."},"warnings":[{"fix":"Prefer composition, inheritance, or dependency injection over monkey patching when possible. Document all monkey patches thoroughly.","message":"Monkey patching should be used with caution as it can lead to hard-to-debug issues and tightly couple your code to internal implementations of third-party libraries.","severity":"gotcha","affected_versions":"All"},{"fix":"If you intend to layer multiple patches, set `avoid_doublewrap=False` in the decorator. Always use `monkeybiz.unpatch()` to revert patches when they are no longer needed, especially in testing contexts, to prevent unexpected side effects.","message":"By default, `avoid_doublewrap=True`, meaning functions and methods can only be patched once using `monkeybiz.patch`. Subsequent attempts to patch the same target will not take effect unless `avoid_doublewrap` is explicitly set to `False`.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure your decorated patch function always has `original_fn` as its first parameter to correctly wrap the original functionality.","message":"The `monkeybiz.patch` decorator expects the function being decorated to accept `original_fn` as its first argument, followed by `*args` and `**kwargs`, to allow calling the original implementation. Failing to include `original_fn` will prevent access to the unpatched function.","severity":"gotcha","affected_versions":"All"},{"fix":"Evaluate the stability and community support before relying heavily on this library for critical production systems. Consider forking if custom modifications or active maintenance are required.","message":"The project's latest version (1.1.0) was uploaded to PyPI on July 11, 2024, but the initial release appears to be from March 2016. This suggests the library may not be under active development, and issues or new feature requests might not be addressed promptly.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}