常见文档字符串列表:pycharm的类型

Rom*_*man 4 python types pycharm

Pycharm有一个很好的功能,它可以读取函数docstring中的类型声明,并使用正确的用法检查函数.

是否有一个很好的资源,它列出了所有常用类型的规则和名称?

注意:
我在下面提供了一个答案,其中包含了我能够找到的所有内容.尽管如此,它还远非全面.

Rom*_*man 12

这是我常用的类型列表:

string:             str
unicode:            unicode
integer:            int
float:              float
general dictionary: dict
general list:       list
general tuple:      tuple
None:               None
not defined:        Any
your own object:    MySpecialClass

specific tuple:     (str, int)
specific list:      list of str
specific dict:      dict[str, int]
multiple options:   str or list
Run Code Online (Sandbox Code Playgroud)

例:

import requests
def example_function(list_var, str_var, num_var, request_var):
    """

    :param list_var:
    :type list_var: list of str
    :param str_var:
    :type str_var: str or unicode
    :param num_var:
    :type num_var: int
    :param request_var:
    :type request_var: requests.models.Request
    :return:
    :rtype: (list of dict) or None
    """
    return [{}]


example_function(
    ['a', 'b'],
    u'unicode accepted because stated',
    1.234,
    requests.Request('GET', 'http://going/somewhere')
)
Run Code Online (Sandbox Code Playgroud)

通过以下模块和类找到了正确的Request格式 __init__.py

然后,当一个人按下Ctrl并悬停在函数调用上时,会得到一个类型帮助文本.这非常有用,因为PEP 8命名约定在设计上是类型模糊的.

在这种情况下,帮助文本将是:

Inferred type: (list_var: List[str], str_var: Union[str, unicode], num_var: int, request_var: Request) -> Optional[List[dict]]
Run Code Online (Sandbox Code Playgroud)

这个num_var论点的警告:

Expected type 'int', got 'float'
Run Code Online (Sandbox Code Playgroud)