Quick answer: what's a monkeypatch
A monkeypatch is when you replace a function (or method, or attribute) on a module at runtime, after it's already been imported, without touching the original source code. The replacement only lives in your process — it doesn't modify the file on disk.
In our case:
import torch
_orig_load = torch.load # save the original
def _trusting_load(*args, **kwargs):
kwargs["weights_only"] = False # force this argument
return _orig_load(*args, **kwargs) # then call the real one
torch.load = _trusting_load # ← the monkeypatch
After that line runs, every call to torch.load(...) anywhere in the process — ours, pyannote's, pytorch_lightning's, anyone's — silently gets routed through _trusting_load, which forces weights_only=False. The torch library itself is unchanged on disk; we just swapped the function pointer in this Python session.
Why "monkey": the term goes back to "guerrilla patch" → "gorilla patch" → "monkey patch." It's mildly pejorative — the idea is it's a sneaky/dirty fix, because callers don't see it coming.
When you should use one:
  • A library you depend on calls torch.load(weights_only=True) explicitly and you can't change its code, but you trust the source enough to bypass the check.
  • A test needs to replace time.time() to make output deterministic.
  • A vendor lib has a known bug and you want to patch it without forking.
When NOT to:
  • If you can change the caller's code instead, do that — monkeypatches are invisible to anyone reading the codebase later.
• • Long-lived monkeypatches in production code accumulate as "magic" no one understands. Each one needs a comment explaining why the underlying library can't just be fixed.
0
0 comments
Guerin Green
5
Quick answer: what's a monkeypatch
⚡Burstiness and Perplexity⚡
skool.com/burstiness-and-perplexity
AI-native SEO, autonomous agents, and automation pipelines. Built for practitioners who build— not collect. Home of the Hidden State Drift Mastermind.
Leaderboard (30-day)
Powered by