|
1 | 1 | import functools
|
2 | 2 | import inspect
|
| 3 | +import itertools |
3 | 4 | import sys
|
4 | 5 | import warnings
|
5 | 6 |
|
@@ -120,15 +121,42 @@ def decorator_apply(dec, func):
|
120 | 121 | dict(decfunc=dec(func)), __wrapped__=func)
|
121 | 122 |
|
122 | 123 |
|
| 124 | +class DecoratorArgumentsError(Exception): |
| 125 | + pass |
| 126 | + |
| 127 | + |
| 128 | +def repr_args_kwargs(*args, **kwargs): |
| 129 | + arguments = ', '.join(itertools.chain( |
| 130 | + (repr(x) for x in args), |
| 131 | + ('{}={}'.format(k, repr(v)) for k, v in kwargs.items()) |
| 132 | + )) |
| 133 | + |
| 134 | + return '({})'.format(arguments) |
| 135 | + |
| 136 | + |
| 137 | +def positional_not_allowed_exception(*args, **kwargs): |
| 138 | + arguments = repr_args_kwargs(**args, **kwargs) |
| 139 | + |
| 140 | + return DecoratorArgumentsError( |
| 141 | + 'Positional decorator arguments not allowed: {}'.format(arguments), |
| 142 | + ) |
| 143 | + |
| 144 | + |
123 | 145 | def _optional_arguments():
|
124 | 146 | def decorator_decorator(d):
|
125 | 147 | # TODO: this should get the signature of d minus the f or something
|
126 |
| - def decorator_wrapper(f=None, **decorator_arguments): |
| 148 | + def decorator_wrapper(*args, **decorator_arguments): |
127 | 149 | """this is decorator_wrapper"""
|
128 |
| - if f is not None: |
129 |
| - if len(decorator_arguments) > 0 or not callable(f): |
130 |
| - raise Exception('positional options not allowed') |
| 150 | + if len(args) > 1: |
| 151 | + raise positional_not_allowed_exception() |
| 152 | + |
| 153 | + if len(args) == 1: |
| 154 | + maybe_f = args[0] |
| 155 | + |
| 156 | + if len(decorator_arguments) > 0 or not callable(maybe_f): |
| 157 | + raise positional_not_allowed_exception() |
131 | 158 |
|
| 159 | + f = maybe_f |
132 | 160 | return d(f)
|
133 | 161 |
|
134 | 162 | # TODO: this should get the signature of d minus the kwargs
|
|
0 commit comments