-
-
Save JrooTJunior/95055a82629739b0ea16ea564902b21b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import inspect | |
def check_spec(func: callable, kwargs: dict): | |
spec = inspect.getfullargspec(func) | |
if spec.varkw: | |
return kwargs | |
return {k: v for k, v in kwargs.items() if k in spec.args} | |
def safe_kwargs(func_or_class): | |
@functools.wraps(func_or_class) | |
def wrap(*args, **kwargs): | |
spec = check_spec(func_or_class, kwargs) | |
return func_or_class(*args, **spec) | |
return wrap | |
@safe_kwargs | |
class Foo: | |
def __init__(self, foo, bar=None): | |
print(foo, bar) | |
Foo(42, bar='asdsad', baz='asdasd') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment