Skip to content

Instantly share code, notes, and snippets.

Created Jan 11, 2019
What would you like to do?
借助 Type Hint 进行运行时类型检查的 Python 装饰器
from functools import wraps
from inspect import getfullargspec
from typing import get_type_hints
def validate_input(hints, **kwargs):
# iterate all type hints
for attr_name, attr_type in hints.items():
if attr_name == 'return':
if not isinstance(kwargs[attr_name], attr_type):
raise TypeError(f'Argument {attr_name} is not of type {attr_type}')
def type_check(decorator):
def wrapped_decorator(*args, **kwargs):
# translate *args into **kwargs
func_args = getfullargspec(decorator)[0]
kwargs.update(dict(zip(func_args, args)))
hints = get_type_hints(decorator)
validate_input(hints, **kwargs)
ret = decorator(**kwargs)
if not isinstance(ret, hints['return']):
raise TypeError(f'Return value is not of type {hints["return"]}')
return ret
return wrapped_decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment