Skip to content

Commit a4a6c0a

Browse files
committed
avoid consuming keyword 'f' args in optional arguments
1 parent 500b558 commit a4a6c0a

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

pytest_twisted.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import inspect
3+
import itertools
34
import sys
45
import warnings
56

@@ -120,15 +121,42 @@ def decorator_apply(dec, func):
120121
dict(decfunc=dec(func)), __wrapped__=func)
121122

122123

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+
123145
def _optional_arguments():
124146
def decorator_decorator(d):
125147
# 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):
127149
"""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()
131158

159+
f = maybe_f
132160
return d(f)
133161

134162
# TODO: this should get the signature of d minus the kwargs

0 commit comments

Comments
 (0)