Registry / testing / python-monkey-business

python-monkey-business

JSON →
library1.1.0pypypi✓ verified 24d ago

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.

pip install python-monkey-business
INSTALL
IMPORT
SIG · PYTHON-MONKEY-BUSI
P
python-monkey-business
testingpythonv1.1.0
Install
1.5s avg
Import
23ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.024s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.022s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

monkeybiz
import monkeybiz
patch
from monkeybiz import patch
Typically used as a decorator: @monkeybiz.patch(...)
unpatch
from monkeybiz import unpatch
Used to revert a monkey-patched function to its original state.

Demonstrates how to use the `monkeybiz.patch` decorator to replace a method within a class and how to revert the patch using `monkeybiz.unpatch`.

import monkeybiz # Assume 'foomodule' and 'FooClass' exist for demonstration class FooClass: def bar(self): return "original" # Patch a method in a class @monkeybiz.patch(FooClass) def bar(original_fn, *args, **kwargs): print("Patched!") return "patched_" + original_fn(*args, **kwargs) instance = FooClass() print(instance.bar()) # Should print 'Patched!' and 'patched_original' monkeybiz.unpatch(FooClass, 'bar') print(instance.bar()) # Should print 'original' again # Example of patching a module-level function (requires a dummy module) # import sys # class DummyModule: # def baz(): return 'original_module_baz' # sys.modules['barmodule'] = DummyModule() # import barmodule # # @monkeybiz.patch(barmodule) # def baz(original_fn, *args, **kwargs): # return 'patched_' + original_fn(*args, **kwargs) # # print(barmodule.baz()) # Should print 'patched_original_module_baz' # monkeybiz.unpatch(barmodule, 'baz')
Debug
Known issues
gotchaMonkey 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.
fix
Prefer composition, inheritance, or dependency injection over monkey patching when possible. Document all monkey patches thoroughly.
affects: All
gotchaBy 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`.
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.
affects: All
gotchaThe `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.
fix
Ensure your decorated patch function always has `original_fn` as its first parameter to correctly wrap the original functionality.
affects: All
gotchaThe 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.
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.
affects: All
Upgrade
Version history
1.1.0latest on PyPI · released Jul 11, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
4
Resources
python-monkey-business — pip install python-monkey-business · libregistry