Skip to content

Instantly share code, notes, and snippets.

@Phate334
Last active December 11, 2020 17:03
Show Gist options
  • Save Phate334/7feee36948fb3cba9dfeb93ca8c771d8 to your computer and use it in GitHub Desktop.
Save Phate334/7feee36948fb3cba9dfeb93ca8c771d8 to your computer and use it in GitHub Desktop.
pydantic and decorator
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