omnipy.util.decorators
Decorator helpers for callbacks, properties, and dual-bound methods.
This module provides lightweight decorator-building utilities used across Omnipy.
The helpers cover common patterns such as fallback callbacks on exceptions,
post-call result transformation, applying decorators to properties, optionally
delegating through super(), and exposing one implementation as both a class
method and an instance method.
| CLASS | DESCRIPTION |
|---|---|
class_or_instance_method |
Descriptor decorator enabling both class and instance invocation styles. |
| FUNCTION | DESCRIPTION |
|---|---|
add_callback_after_call |
Return a wrapper that post-processes a successful result. |
add_callback_if_exception |
Return a wrapper that falls back to |
apply_decorator_to_property |
Apply the same decorator to all available accessors of a property. |
call_super_if_available |
Create a descriptor that composes a method with its |
| ATTRIBUTE | DESCRIPTION |
|---|---|
T |
|
no_context |
|
T
module-attribute
-
Reference
Code reference
omnipy
util
decoratorsclass_or_instance_method
-
Reference
Code reference
omnipy
util
decoratorsclass_or_instance_method__init__
class_or_instance_method
Bases: Generic[T, _DecoratedP, _DecoratedR], object
flowchart BT
omnipy.util.decorators.class_or_instance_method[class_or_instance_method]
click omnipy.util.decorators.class_or_instance_method href "" "omnipy.util.decorators.class_or_instance_method"
Descriptor decorator enabling both class and instance invocation styles.
The wrapped implementation receives both instance and cls so it can
branch behavior depending on whether it was called via obj.method(...) or
Cls.method(...).
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Store the dual-bound implementation function. |
Source code in src/omnipy/util/decorators.py
add_callback_after_call
add_callback_after_call(
decorated_func: Callable[_DecoratedP, _DecoratedR],
callback_func: Callable[Concatenate[_DecoratedR, _CallbackP], _DecoratedR],
with_context: ContextManager[None] | None,
*cb_args: _CallbackP.args,
**cb_kwargs: _CallbackP.kwargs,
) -> Callable[_DecoratedP, _DecoratedR]
Return a wrapper that post-processes a successful result.
| PARAMETER | DESCRIPTION |
|---|---|
decorated_func
|
Callable producing the initial result.
TYPE:
|
callback_func
|
Callable receiving the result plus callback arguments.
TYPE:
|
with_context
|
Optional context manager wrapped around the full call.
TYPE:
|
*cb_args
|
Extra positional callback arguments.
TYPE:
|
**cb_kwargs
|
Extra keyword callback arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable[_DecoratedP, _DecoratedR]
|
A wrapped callable that runs |
Source code in src/omnipy/util/decorators.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
add_callback_if_exception
add_callback_if_exception(
decorated_func: Callable[_DecoratedP, _DecoratedR],
callback_func: Callable[_DecoratedP, _DecoratedR],
exception_types: tuple[type[Exception], ...] = (Exception,),
) -> Callable[_DecoratedP, _DecoratedR]
Return a wrapper that falls back to callback_func on selected exceptions.
| PARAMETER | DESCRIPTION |
|---|---|
decorated_func
|
Primary callable.
TYPE:
|
callback_func
|
Fallback callable invoked with the same arguments.
TYPE:
|
exception_types
|
Exception types that trigger the fallback.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable[_DecoratedP, _DecoratedR]
|
A wrapped callable with exception-triggered fallback behavior. |
Source code in src/omnipy/util/decorators.py
apply_decorator_to_property
Apply the same decorator to all available accessors of a property.
| PARAMETER | DESCRIPTION |
|---|---|
prop
|
Property whose getter, setter, and deleter should be decorated.
TYPE:
|
decorator
|
Decorator applied to each existing accessor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
property
|
A new property with decorated accessors and the original docstring. |
Source code in src/omnipy/util/decorators.py
call_super_if_available
Create a descriptor that composes a method with its super() version.
| PARAMETER | DESCRIPTION |
|---|---|
call_super_before_method
|
When
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
A descriptor class wrapping a single-argument instance or class method. |
Notes
If the parent class has no same-named callable, the original method is executed directly.
Source code in src/omnipy/util/decorators.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |