Last active
December 11, 2020 17:03
-
-
Save Phate334/7feee36948fb3cba9dfeb93ca8c771d8 to your computer and use it in GitHub Desktop.
pydantic and decorator
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
from pydantic import BaseModel | |
def func_helper(parameter_type, return_type=None): | |
def decorator(function): | |
def wrapper(*args, **kwargs): | |
result = function(parameter_type(**kwargs)) | |
return return_type(**result) if return_type else result | |
return wrapper | |
return decorator | |
class Parameters(BaseModel): | |
a: str | |
b: str = 'b' | |
c: int = 0 | |
class ReturnValue(BaseModel): | |
message: str | |
@func_helper(Parameters, ReturnValue) | |
def run(parms: Parameters): | |
return {'message': parms.json()} | |
print(run(a='a', c='1', test='1')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment