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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.046s · 18.1MB
glibcpy 3.10–3.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}")
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.
fixAvoid 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.
fixVerify 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.
fixEnsure 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.
fixEnsure 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.