VS Code 如何获得类型提示? [英] How does VS Code get type hints?

查看:38
本文介绍了VS Code 如何获得类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中使用 argparse 时注意到以下内容:

I was using argparse in a project when I noticed the following:

  • VS Code 显示了 ArgumentParser.add_argument 参数的类型提示:

add_argument(*name_or_flags: Text, action: Union[Text, Type[Action]]=..., nargs: Union[int, Text]=..., const: Any=..., default: Any=..., type: Union[Callable[[Text], _T], Callable[[str], _T], FileType]=..., choices: Iterable[_T]=..., required: bool=..., help: Optional[Text]=..., metavar: Optional[Union[Text, Tuple[Text, ...]]]=..., dest: Optional[Text]=..., version: Text=..., **kwargs: Any) -> Action
param *name_or_flags: Text

add_argument(dest, ..., name=value, ...)
add_argument(option_string, option_string, ..., name=value, ...)

  • argparse 的源代码没有这些提示(https://github.com/python/cpython/blob/master/Lib/argparse.py ):

        # =======================
        # Adding argument actions
        # =======================
        def add_argument(self, *args, **kwargs):
            """
            add_argument(dest, ..., name=value, ...)
            add_argument(option_string, option_string, ..., name=value, ...)
            """
    
            # if no positional args are supplied or only one is supplied and
            # it doesn't look like an option string, parse a positional
            # argument
    

  • 我将如何在自己的代码中编写一个以相同方式显示类型提示的函数(即我编写的​​函数具有 *args 和 **kwargs 作为参数,但该函数的任何用户都可以看到名称和类型预期的 kwargs)?

    How would I write a function in my own code that shows type hints in the same way (i.e. the function I write has *args and **kwargs as the arguments, but any user of the function can see the names and types of the expected kwargs)?

    推荐答案

    该类型信息来自 typeshed:

    Typeshed 包含 Python 标准的外部类型注释库和 Python 内置程序,以及第三方包由这些项目的外部人员贡献.

    Typeshed contains external type annotations for the Python standard library and Python builtins, as well as third party packages as contributed by people external to those projects.

    此数据可以例如用于静态分析、类型检查或类型推论.

    This data can e.g. be used for static analysis, type checking or type inference.

    你可以看到add_argument的具体类型这里:

    You can see the specific types for add_argument here:

    def add_argument(self,
                     *name_or_flags: Text,
                     action: Union[Text, Type[Action]] = ...,
                     nargs: Union[int, Text] = ...,
                     const: Any = ...,
                     default: Any = ...,
                     type: Union[Callable[[Text], _T], Callable[[str], _T], FileType] = ...,
                     choices: Iterable[_T] = ...,
                     required: bool = ...,
                     help: Optional[Text] = ...,
                     metavar: Optional[Union[Text, Tuple[Text, ...]]] = ...,
                     dest: Optional[Text] = ...,
                     version: Text = ...,
                     **kwargs: Any) -> Action: ...
    

    对于您自己的代码,您可以直接添加这些类型提示,除非您需要支持旧版本的 Python(语法从 3.0 开始,每个 PEP-3107typing 模块可从 3.5 每个 PEP-0484).

    For your own code, you can just add those type hints in directly, unless you need to support older versions of Python (syntax available from 3.0 per PEP-3107, typing module available from 3.5 per PEP-0484).

    这篇关于VS Code 如何获得类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆