mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
call_with_supported_arguments() util, refs #581
This commit is contained in:
parent
9e6075d21f
commit
41a0cd7b6a
2 changed files with 22 additions and 0 deletions
|
|
@ -3,6 +3,7 @@ from collections import OrderedDict
|
||||||
import base64
|
import base64
|
||||||
import click
|
import click
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import inspect
|
||||||
import json
|
import json
|
||||||
import mergedeep
|
import mergedeep
|
||||||
import os
|
import os
|
||||||
|
|
@ -803,3 +804,13 @@ def parse_metadata(content):
|
||||||
return yaml.safe_load(content)
|
return yaml.safe_load(content)
|
||||||
except yaml.YAMLError:
|
except yaml.YAMLError:
|
||||||
raise BadMetadataError("Metadata is not valid JSON or YAML")
|
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)
|
request = Request(scope, receive)
|
||||||
assert {"foo": "bar", "baz": "1"} == await request.post_vars()
|
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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue