Registry / testing / python-monkey-business

python-monkey-business

JSON →
library1.1.0pypypi✓ verified 35d 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.

testing
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.920 runs
installs and imports cleanly · install 0.0s · import 0.024s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.023s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

import monkeybiz

Typically used as a decorator: @monkeybiz.patch(...)

from monkeybiz import patch

Used to revert a monkey-patched function to its original state.

from monkeybiz import unpatch

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 footguns
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.
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`.
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.
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.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
15 hits · last 30 days
gptbot
4
ahrefsbot
4
bytedance
3
script
1
Resources