typingplus.py 文件源码

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

项目:typingplus 作者: contains-io 项目源码 文件源码
def get_type_hints(obj,  # type: Any
                   globalns=None,  # type: Optional[Dict[str, Any]]
                   localns=None  # type: Optional[Dict[str, Any]]
                   ):
    # type: (...) -> Dict[str, Any]
    """Return all type hints for the function.

    This attempts to use typing.get_type_hints first, but if that returns None
    then it will attempt to reuse much of the logic from the Python 3 version
    of typing.get_type_hints; the Python 2 version does nothing. In addition to
    this logic, if no code annotations exist, it will attempt to extract
    comment type hints for Python 2/3 compatibility.

    Args:
        obj: The object to search for type hints.
        globalns: The currently known global namespace.
        localns: The currently known local namespace.

    Returns:
        A mapping of value names to type hints.
    """
    hints = {}
    try:
        if not isinstance(obj, type):
            hints = _get_type_hints(obj, globalns, localns) or {}
    except TypeError:
        if not isinstance(obj, _STRING_TYPES):
            raise
    if not hints and not getattr(obj, '__no_type_check__', None):
        globalns, localns = _get_namespace(obj, globalns, localns)
        hints = _get_comment_type_hints(obj, globalns, localns)
        for name, value in six.iteritems(hints):
            if value is None:
                value = type(None)
            elif isinstance(value, _STRING_TYPES):
                value = _ForwardRef(value)
            hints[name] = _eval_type(value, globalns, localns)
    return hints
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号