Registry / testing / decopatch

decopatch

JSON →
library1.4.10pypypi✓ verified 23d ago

decopatch is a Python library designed to simplify the creation of decorators. It addresses the common challenge in Python where writing decorators requires explicit handling of both with-parenthesis and without-parenthesis usages. The library is currently at version 1.4.10 and receives regular minor updates, maintaining an active development status.

pip install decopatch
INSTALL
IMPORT
SIG · DECOPATCH
D
decopatch
testingpythonv1.4.10
Install
1.6s avg
Import
43ms
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.4.10 · 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.046s · 18.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.040s · 19MB
16MB installed
● package 16MB
Code
Verified usage

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

function_decorator
from decopatch import function_decorator
Primary decorator factory for functions.
class_decorator
from decopatch import class_decorator
Decorator factory for classes.
decorator
from decopatch import decorator
Generic decorator factory for decorators that can be applied to both functions and classes. `function_decorator` and `class_decorator` are presets for this.
WRAPPED, F_ARGS, F_KWARGS
from decopatch import WRAPPED, F_ARGS, F_KWARGS
Symbols used in 'double-flat mode' for more compact decorator definitions, representing the decorated item, positional arguments, and keyword arguments respectively.

This quickstart demonstrates the `function_decorator` to create a decorator that seamlessly works with and without parentheses, as well as with explicit arguments. The decorator `add_tag` adds a `tag` attribute to the decorated function.

from decopatch import function_decorator @function_decorator def add_tag(tag='hi!'): """ Example decorator to add a 'tag' attribute to a function. :param tag: the 'tag' value to set on the decorated function. """ def _apply_decorator(f): """ This method is called when `@add_tag` is used on a function `f`. It should return a replacement for `f`. """ setattr(f, 'tag', tag) return f return _apply_decorator # Usage without parenthesis @add_tag def foo1(): pass assert foo1.tag == 'hi!' # Usage with empty parenthesis @add_tag() def foo2(): pass assert foo2.tag == 'hi!' # Usage with arguments @add_tag('hello') def foo3(): pass assert foo3.tag == 'hello' print(f"foo1 tag: {foo1.tag}") print(f"foo2 tag: {foo2.tag}") print(f"foo3 tag: {foo3.tag}")
Debug
Known issues
breakingEnabling the experimental 'stack introspection' feature (`enable_stack_introspection=True`) now raises an explicit `NotImplementedError` on Python 3.8 and newer versions. Users relying on this beta feature on recent Python versions will encounter errors.
fix
Avoid using `enable_stack_introspection=True` on Python 3.8+ or consult documentation for alternatives if introspection is critical.
affects: 1.4.9+
gotchaWhile `decopatch` significantly simplifies decorator creation, prior to version 1.4.6, there were reported bugs concerning incorrect injection of arguments, particularly when variable-positional arguments (`*args`) were present in the decorated function's signature. Although fixed, this highlights a potential 'gotcha' area for complex function signatures if not fully leveraging `decopatch`'s mechanisms or if custom wrapper logic is introduced without careful consideration.
fix
Ensure `decopatch` is updated to at least version 1.4.6 to benefit from fixes related to variable-positional argument handling. Rely primarily on `decopatch`'s provided constructs for decorator creation to avoid manual signature manipulation errors.
affects: <1.4.6
gotchaThe primary problem `decopatch` solves is enabling decorators to work with or without parentheses seamlessly. Manually attempting to detect how a decorator was called (e.g., by checking `callable(args[0])`) is a common and error-prone workaround in Python. Using `decopatch`'s `function_decorator`, `class_decorator`, or `decorator` correctly abstracts this complexity, preventing a significant footgun for decorator authors.
fix
Always use `decopatch`'s decorator factories (`@function_decorator`, `@class_decorator`, `@decorator`) as intended to handle the parenthesis detection automatically, rather than implementing manual checks within your decorator logic.
affects: All versions
Errors
Common errors & fixes
NotImplementedError: stack introspection is not supported on Python 3.8+
The experimental 'stack introspection' feature (`enable_stack_introspection=True`) in decopatch is not compatible with Python versions 3.8 and newer, and explicitly raises this error when used.
fix
Avoid using `enable_stack_introspection=True` in your decopatch configuration on Python 3.8+; remove the argument or consult the documentation for alternative approaches if introspection is critical.
ImportError: cannot import name 'function_decorator' from 'decopatch'
This error occurs when Python cannot find the specified name (e.g., `function_decorator`, `class_decorator`, `decorator`, `DECORATED`) within the `decopatch` library. This can be due to a typo in the import statement or the library not being correctly installed or being an outdated version that lacks the imported symbol.
fix
Verify the spelling of the imported name, ensure `decopatch` is installed (`pip install decopatch`), and confirm that your `decopatch` version supports the component you are trying to import.
TypeError: 'function' object is not a valid decorator factory result
This `TypeError` can occur if a decopatch decorator factory is defined in a way that it directly returns the decorated function or an improper callable when it is expected to return another callable (the actual decorator) or explicitly use a different return mechanism provided by decopatch. It indicates that the value returned by the decorator factory, when invoked with or without parentheses, is not understood by decopatch as a valid step in the decoration process, usually when the internal structure for handling the decorated function is incorrect.
fix
Ensure your decopatch decorator factory (`@function_decorator`, `@class_decorator`, `@decorator`) returns the `_apply_decorator` inner function as intended, or correctly uses `DECORATED` in flat mode to receive the decorated function. Do not prematurely return the decorated function itself from the decorator factory.
TypeError: 'builtin_function_or_method' object is not iterable
This error can arise if `decopatch`'s `DECORATED` symbol is misused, for example, by attempting to iterate over it directly when it is passed as a placeholder for the decorated function, or if a decorated function is invoked with incorrect arguments due to a misunderstanding of how `decopatch` adjusts the signature or argument passing.
fix
Ensure that the `DECORATED` symbol is correctly assigned to a parameter in your decorator function's signature (e.g., `f=DECORATED`) and handled as the decorated callable within your decorator logic, rather than attempting to iterate over `DECORATED` directly. Also, verify that the decorated function is called with the expected arguments within your decorator's wrapper.
Upgrade
Version history
1.4.10latest on PyPI · released Mar 1, 2022
Audit
Dependencies
makefunoptionalAn optional companion library that works with decopatch to create truly signature-preserving function wrappers, solving problems that decopatch focuses on decorator syntax handling.
Agent activity
20 hits · last 30 days
node
18
Resources
decopatch — pip install decopatch · libregistry