我如何告诉 PyCharm 参数应该是什么类型? [英] How can I tell PyCharm what type a parameter is expected to be?

查看:36
本文介绍了我如何告诉 PyCharm 参数应该是什么类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当涉及到构造函数、赋值和方法调用时,PyCharm IDE 非常擅长分析我的源代码并确定每个变量应该是什么类型.我喜欢它正确的时候,因为它给了我很好的代码完成和参数信息,如果我尝试访问一个不存在的属性,它会给我警告.

When it comes to constructors, and assignments, and method calls, the PyCharm IDE is pretty good at analyzing my source code and figuring out what type each variable should be. I like it when it's right, because it gives me good code-completion and parameter info, and it gives me warnings if I try to access an attribute that doesn't exist.

但是当涉及到参数时,它什么都不知道.代码完成下拉菜单无法显示任何内容,因为它们不知道参数的类型.代码分析无法查找警告.

But when it comes to parameters, it knows nothing. The code-completion dropdowns can't show anything, because they don't know what type the parameter will be. The code analysis can't look for warnings.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

peasant = Person("Dennis", 37)
# PyCharm knows that the "peasant" variable is of type Person
peasant.dig_filth()   # shows warning -- Person doesn't have a dig_filth method

class King:
    def repress(self, peasant):
        # PyCharm has no idea what type the "peasant" parameter should be
        peasant.knock_over()   # no warning even though knock_over doesn't exist

King().repress(peasant)
# Even if I call the method once with a Person instance, PyCharm doesn't
# consider that to mean that the "peasant" parameter should always be a Person

这有一定的意义.其他呼叫站点可以为该参数传递任何内容.但是,如果我的方法需要一个参数类型,比如 pygame.Surface,我希望能够以某种方式向 PyCharm 表明这一点,这样它就可以向我显示所有 Surface 在其代码完成下拉列表中的属性,并在我调用错误方法时突出显示警告,等等.

This makes a certain amount of sense. Other call sites could pass anything for that parameter. But if my method expects a parameter to be of type, say, pygame.Surface, I'd like to be able to indicate that to PyCharm somehow, so it can show me all of Surface's attributes in its code-completion dropdown, and highlight warnings if I call the wrong method, and so on.

有没有办法可以给 PyCharm 一个提示,并说psst,这个参数应该是 X 类型"?(或者,本着动态语言的精神,这个参数应该像 X 一样嘎嘎作响"?我会接受的.)

Is there a way I can give PyCharm a hint, and say "psst, this parameter is supposed to be of type X"? (Or perhaps, in the spirit of dynamic languages, "this parameter is supposed to quack like an X"? I'd be fine with that.)

CrazyCoder 的回答如下,可以解决问题.对于像我这样想要快速总结的新手,这里是:

CrazyCoder's answer, below, does the trick. For any newcomers like me who want the quick summary, here it is:

class King:
    def repress(self, peasant):
        """
        Exploit the workers by hanging on to outdated imperialist dogma which
        perpetuates the economic and social differences in our society.

        @type peasant: Person
        @param peasant: Person to repress.
        """
        peasant.knock_over()   # Shows a warning. And there was much rejoicing.

相关部分是文档字符串的@type农:Person行.

The relevant part is the @type peasant: Person line of the docstring.

如果你也去文件 >设置 >Python 集成工具和设置Docstring 格式"到Epytext",然后是 PyCharm 的视图 >快速文档查找将漂亮地打印参数信息,而不是按原样打印所有 @-lines.

If you also go to File > Settings > Python Integrated Tools and set "Docstring format" to "Epytext", then PyCharm's View > Quick Documentation Lookup will pretty-print the parameter information instead of just printing all the @-lines as-is.

推荐答案

是的,您可以为方法及其参数使用特殊的文档格式,以便 PyCharm 可以知道类型.最近的 PyCharm 版本 支持最常见的文档格式.

Yes, you can use special documentation format for methods and their parameters so that PyCharm can know the type. Recent PyCharm version supports most common doc formats.

例如,PyCharm 从 @param 样式注释 中提取类型.

For example, PyCharm extracts types from @param style comments.

另见 reStructuredText文档字符串约定 (PEP 257).

See also reStructuredText and docstring conventions (PEP 257).

另一个选项是 Python 3 注释.

Another option is Python 3 annotations.

参考 PyCharm 文档部分更多细节和示例.

这篇关于我如何告诉 PyCharm 参数应该是什么类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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