kopia lustrzana https://github.com/simonw/datasette
call_with_supported_arguments() util, refs #581
rodzic
9e6075d21f
commit
41a0cd7b6a
|
@ -3,6 +3,7 @@ from collections import OrderedDict
|
|||
import base64
|
||||
import click
|
||||
import hashlib
|
||||
import inspect
|
||||
import json
|
||||
import mergedeep
|
||||
import os
|
||||
|
@ -803,3 +804,13 @@ def parse_metadata(content):
|
|||
return yaml.safe_load(content)
|
||||
except yaml.YAMLError:
|
||||
raise BadMetadataError("Metadata is not valid JSON or YAML")
|
||||
|
||||
|
||||
def call_with_supported_arguments(fn, **kwargs):
|
||||
parameters = inspect.signature(fn).parameters.keys()
|
||||
call_with = []
|
||||
for parameter in parameters:
|
||||
if parameter not in kwargs:
|
||||
raise TypeError("{} requires parameters {}".format(fn, tuple(parameters)))
|
||||
call_with.append(kwargs[parameter])
|
||||
return fn(*call_with)
|
||||
|
|
|
@ -446,3 +446,14 @@ async def test_request_post_vars():
|
|||
|
||||
request = Request(scope, receive)
|
||||
assert {"foo": "bar", "baz": "1"} == await request.post_vars()
|
||||
|
||||
|
||||
def test_call_with_supported_arguments():
|
||||
def foo(a, b):
|
||||
return "{}+{}".format(a, b)
|
||||
|
||||
assert "1+2" == utils.call_with_supported_arguments(foo, a=1, b=2)
|
||||
assert "1+2" == utils.call_with_supported_arguments(foo, a=1, b=2, c=3)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
utils.call_with_supported_arguments(foo, a=1)
|
||||
|
|
Ładowanie…
Reference in New Issue