typed.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:tsukkomi 作者: spoqa 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号