def typechecked(call_: typing.Callable[..., T]) -> T:
"""A decorator to make a callable object checks its types
.. code-block:: python
from typing import Callable
@typechecked
def foobar(x: str) -> bool:
return x == 'hello world'
@typechecked
def hello_world(foo: str, bar: Callable[[str], bool]) -> bool:
return bar(foo)
hello_world('hello world', foobar)
hello_world(3.14, foobar) # it raise TypeError
:param c: callable object want to check types
:type c: :class:`typing.Callable`
:return:
"""
@functools.wraps(call_)
def decorator(*args, **kwargs):
hints = typing.get_type_hints(call_)
check_arguments(call_, hints, *args, **kwargs)
result = call_(*args, **kwargs)
check_return(call_.__name__, result, hints)
return result
return decorator
评论列表
文章目录