Decorators
Miscellaneous decorators used throughout the library.
Functions
typecheck(func_=None, **types)
Decorator to enforce type checking for a function or method. There are two ways to call this: either explicitly passing argument types to the decorator, or letting it infer them using type annotations in the function that will be decorated. We allow both usage methods since older versions of Python lack type annotations, and also because I feel the annotation syntax can hurt readability.
Ported from htools to avoid extra dependency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func_ |
Optional[Callable]
|
The function to decorate. When using decorator with
manually-specified types, this is None. Underscore is used so that
|
None
|
types |
Dict[str, Type]
|
Optional way to specify variable types. Use standard types rather than
importing from the typing library, as subscripted generics are not
supported (e.g. typing.List[str] will not work; typing.List will but at
that point there is no benefit over the standard |
{}
|
Returns:
Type | Description |
---|---|
Callable
|
The decorated function with type checking. |
Examples:
In the first example, we specify types directly in the decorator. Notice that they can be single types or tuples of types. You can choose to specify types for all arguments or just a subset.
@typecheck(x=float, y=(int, float), iters=int, verbose=bool)
def process(x, y, z, iters=5, verbose=True):
print(f'z = {z}')
for i in range(iters):
if verbose: print(f'Iteration {i}...')
x *= y
return x
>>> process(3.1, 4.5, 0, 2.0)
TypeError: iters must be <class 'int'>, not <class 'float'>.
>>> process(3.1, 4, 'a', 1, False)
z = a
12.4
Alternatively, you can let the decorator infer types using annotations in the function that is to be decorated. The example below behaves equivalently to the explicit example shown above. Note that annotations regarding the returned value are ignored.
@typecheck
def process(x:float, y:(int, float), z, iters:int=5, verbose:bool=True):
print(f'z = {z}')
for i in range(iters):
if verbose: print(f'Iteration {i}...')
x *= y
return x
>>> process(3.1, 4.5, 0, 2.0)
TypeError: iters must be <class 'int'>, not <class 'float'>.
>>> process(3.1, 4, 'a', 1, False)
z = a
12.4
Source code in lib/roboduck/decorators.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 |
|
add_kwargs(func, fields, hide_fields=(), strict=False)
Decorator that adds parameters into the signature and docstring of a function that accepts **kwargs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
function
|
Function to decorate. |
required |
fields |
list[str]
|
Names of params to insert into signature + docstring. |
required |
hide_fields |
list[str]
|
Names of params that are already in the function's signature that we want to hide. To use a non-empty value here, we must set strict=True and the param must have a default value, as this is what will be used in all subsequent calls. |
()
|
strict |
bool
|
If true, we do two things:
1. On decorated function call, check that the user provided all
expected arguments.
2. Enable the use of the |
False
|
Returns:
Type | Description |
---|---|
function
|
|
Source code in lib/roboduck/decorators.py
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 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 |
|
store_class_defaults(cls=None, attr_filter=None)
Class decorator that stores default values of class attributes (can be all or a subset). Default here refers to the value at class definition time. Mutable defaults should be okay since we deepcopy them, but are probably still riskier to use than immutable defaults.
Examples:
@store_class_defaults(attr_filter=lambda x: x.startswith('last_'))
class Foo:
last_bar = 3
last_baz = 'abc'
other = True
>>> Foo._class_defaults
{'last_bar': 3, 'last_baz': 'abc'}
Or use the decorator without parentheses to store all values at definition time. This is usually unnecessary. If you do provide an attr_filter, it must be a named argument.
Foo.reset_class_vars() will reset all relevant class vars to their default values.
Source code in lib/roboduck/decorators.py
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
add_docstring(func)
Add the docstring from another function/class to the decorated function/class.
Ported from htools to avoid extra dependency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
function
|
Function to decorate. |
required |
Examples:
@add_docstring(nn.Conv2d)
class ReflectionPaddedConv2d(nn.Module):
# ...
Source code in lib/roboduck/decorators.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|