mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-24 01:24:26 +01:00
pylightning: Exception if we have unfulfilled positional arguments
This caused me to backtrack quite a bit, so this should help debugging in the future. Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
committed by
Rusty Russell
parent
81fa247d07
commit
8de1a85ac0
@@ -252,7 +252,7 @@ class Plugin(object):
|
|||||||
|
|
||||||
arguments = OrderedDict()
|
arguments = OrderedDict()
|
||||||
for name, value in sig.parameters.items():
|
for name, value in sig.parameters.items():
|
||||||
arguments[name] = inspect.Signature.empty
|
arguments[name] = inspect._empty
|
||||||
|
|
||||||
# Fill in any injected parameters
|
# Fill in any injected parameters
|
||||||
if 'plugin' in arguments:
|
if 'plugin' in arguments:
|
||||||
@@ -267,7 +267,7 @@ class Plugin(object):
|
|||||||
else:
|
else:
|
||||||
pos = 0
|
pos = 0
|
||||||
for k, v in arguments.items():
|
for k, v in arguments.items():
|
||||||
if v is not inspect.Signature.empty:
|
if v != inspect._empty:
|
||||||
continue
|
continue
|
||||||
if pos < len(params):
|
if pos < len(params):
|
||||||
# Apply positional args if we have them
|
# Apply positional args if we have them
|
||||||
@@ -280,6 +280,15 @@ class Plugin(object):
|
|||||||
arguments[k] = sig.parameters[k].default
|
arguments[k] = sig.parameters[k].default
|
||||||
pos += 1
|
pos += 1
|
||||||
|
|
||||||
|
missing = [k for k, v in arguments.items() if v == inspect._empty]
|
||||||
|
if missing:
|
||||||
|
raise TypeError("Missing positional arguments ({given} given, "
|
||||||
|
"expected {expected}): {missing}".format(
|
||||||
|
missing=", ".join(missing),
|
||||||
|
given=len(arguments) - len(missing),
|
||||||
|
expected=len(arguments)
|
||||||
|
))
|
||||||
|
|
||||||
ba = sig.bind(**arguments)
|
ba = sig.bind(**arguments)
|
||||||
ba.apply_defaults()
|
ba.apply_defaults()
|
||||||
return func(*ba.args, **ba.kwargs)
|
return func(*ba.args, **ba.kwargs)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from .plugin import Plugin, Request
|
from .plugin import Plugin, Request
|
||||||
import itertools
|
import itertools
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def test_positional_inject():
|
def test_positional_inject():
|
||||||
@@ -42,9 +43,28 @@ def test_positional_inject():
|
|||||||
"""
|
"""
|
||||||
assert (a, b, c, d, e) == (1, 2, 3, 4, 42)
|
assert (a, b, c, d, e) == (1, 2, 3, 4, 42)
|
||||||
|
|
||||||
|
def count(plugin, count, request):
|
||||||
|
assert count == 42 and plugin == p
|
||||||
|
|
||||||
funcs = [pre_args, in_args, post_args, post_kwargs, in_multi_args]
|
funcs = [pre_args, in_args, post_args, post_kwargs, in_multi_args]
|
||||||
|
|
||||||
for func, request in itertools.product(funcs, [rdict, rarr]):
|
for func, request in itertools.product(funcs, [rdict, rarr]):
|
||||||
p._exec_func(func, request)
|
p._exec_func(func, request)
|
||||||
|
|
||||||
p._exec_func(extra_def_arg, rarr)
|
p._exec_func(extra_def_arg, rarr)
|
||||||
|
|
||||||
|
p._exec_func(count, Request(
|
||||||
|
plugin=p,
|
||||||
|
req_id=1,
|
||||||
|
method='func',
|
||||||
|
params=[42],
|
||||||
|
))
|
||||||
|
|
||||||
|
# This should fail since it is missing one positional argument
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
p._exec_func(count, Request(
|
||||||
|
plugin=p,
|
||||||
|
req_id=1,
|
||||||
|
method='func',
|
||||||
|
params=[])
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user