wrapt
Module for decorators, wrappers and monkey patching.
This package has a good security score with no known vulnerabilities.
Community Reviews
Rock-solid foundation for production decorators and monkey patching
What sets wrapt apart operationally is its robustness under real-world conditions. The ObjectProxy implementation properly delegates attribute access, which is critical when monkey patching third-party libraries for observability. I've used it to instrument database clients and HTTP libraries without breaking introspection or causing memory leaks from circular references. The synchronized decorator has been reliable for thread-safety patterns without the boilerplate of manual lock management.
The API surface is minimal and stable—I've upgraded across multiple versions without breaking changes. Documentation is thorough with practical examples for each use case. Error messages are clear when wrappers are misapplied, and the library degrades gracefully to pure Python if the C extension isn't available.
Best for: Production systems requiring robust decorators, instrumentation, or monkey patching with minimal performance impact.
Avoid if: You only need simple function wrapping and don't care about preserving signatures or descriptor protocols.
Rock-solid decorator foundation with zero runtime overhead surprises
The ObjectProxy is exceptionally useful for lazy initialization and monkey patching scenarios. We use it to wrap database connections and API clients, deferring expensive initialization until first access. Memory overhead is minimal, and the proxy behavior is transparent enough that it doesn't break isinstance checks or attribute access patterns. Error messages when something goes wrong are clear and point to the actual issue, not proxy internals.
Configuration is straightforward—it's a library, not a framework. The decorator factory pattern is well-documented with practical examples. Breaking changes between versions have been rare and well-communicated. My only gripe is that advanced proxy customization requires reading source code, but for 95% of use cases, the defaults are exactly what you need.
Best for: Building production-grade decorators, instrumentation layers, and lazy initialization wrappers where performance and correctness matter.
Avoid if: You need simple one-off decorators where standard library functools.wraps is sufficient for your use case.
Robust decorator infrastructure with solid security defaults
The library's approach to wrapper transparency is excellent - wrapped objects behave correctly with isinstance checks and attribute access, preventing subtle bugs where security decorators might be bypassed due to type checking issues. The C extension provides performance benefits without compromising safety, and it gracefully falls back to pure Python when unavailable.
From a security perspective, wrapt doesn't introduce surface area for injection attacks or unsafe defaults. Error messages are appropriately scoped without leaking implementation details. The monkey patching utilities are explicit and trackable, which helps during security audits. My main concern is the minimal documentation around edge cases with complex descriptor protocols, which can lead to unexpected behavior if you're wrapping security-critical class methods.
Best for: Building production-grade decorators for authentication, authorization, and input validation where signature preservation matters.
Avoid if: You need simple function wrappers without introspection requirements - functools.wraps may suffice.
Sign in to write a review
Sign In