You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
633 B
21 lines
633 B
3 years ago
|
import inspect
|
||
|
|
||
|
|
||
|
def check_kwargs(func, kwargs, name=None):
|
||
|
"""check kwargs are valid for func
|
||
|
|
||
|
If kwargs are invalid, raise TypeError as same as python default
|
||
|
:param function func: function to be validated
|
||
|
:param dict kwargs: keyword arguments for func
|
||
|
:param str name: name used in TypeError (default is func name)
|
||
|
"""
|
||
|
try:
|
||
|
params = inspect.signature(func).parameters
|
||
|
except ValueError:
|
||
|
return
|
||
|
if name is None:
|
||
|
name = func.__name__
|
||
|
for k in kwargs.keys():
|
||
|
if k not in params:
|
||
|
raise TypeError(f"{name}() got an unexpected keyword argument '{k}'")
|