Pycharm具有一个不错的功能,即它读取函数docstring中的类型声明,并在使用该函数的位置进行正确用法的检查。

有没有很好的资源,其中列出了所有常用类型的规则和名称?

注意:
我在下面提供了答案,并提供了所有我能弄清楚的内容。但是,它远非全面。

最佳答案

这是我常用的类型列表:

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

例子:
import requests
def example_function(list_var, str_var, num_var, request_var):
    """One line function explanation.

    :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')
)

通过遵循__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]]

并为num_var参数提供警告:
Expected type 'int', got 'float'

关于python - 常见文档字符串:types for pycharm的列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40149689/

10-12 22:37
查看更多