我如何告诉PyCharm参数的类型是什么?

Joe*_*ite 169 python type-hinting code-completion pycharm

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

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

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
Run Code Online (Sandbox Code Playgroud)

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

有没有办法可以给PyCharm一个提示,并说"psst,这个参数应该是X型"?(或者,或许,在动态语言的精神中,"这个参数应该像X一样嘎嘎"?我会好的.)


编辑: CrazyCoder的答案,下面,诀窍.对于像我这样想要快速总结的新手,这里是:

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.
Run Code Online (Sandbox Code Playgroud)

相关部分是@type peasant: Persondocstring 的行.

如果您还转到文件>设置> Python集成工具并将"文档字符串格式"设置为"Epytext",那么PyCharm的View> Quick Documentation Lookup将打印参数信息,而不是仅按原样打印所有@ -lines.

Cra*_*der 84

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

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

另请参阅reStructuredTextdocstring约定(PEP 257).

另一个选项是Python 3注释.

有关更多详细信息和示例,请参阅PyCharm文档部分.

  • 我认为PyCharm改变了它的doc格式(参见https://www.jetbrains.com/help/pycharm/using-docstrings-to-specify-types.html),但谢谢!缺乏对参数的智能感知让我疯狂. (2认同)

Feu*_*mel 46

如果您使用的是Python 3.0或更高版本,则还可以对函数和参数使用注释.PyCharm会将这些解释为参数或返回值预期具有的类型:

class King:
    def repress(self, peasant: Person) -> bool:
        peasant.knock_over() # Shows a warning. And there was much rejoicing.

        return peasant.badly_hurt() # Lets say, its not known from here that this method will always return a bool
Run Code Online (Sandbox Code Playgroud)

有时这对非公共方法很有用,不需要docstring.作为额外的好处,可以通过代码访问这些注释:

>>> King.repress.__annotations__
{'peasant': <class '__main__.Person'>, 'return': <class 'bool'>}
Run Code Online (Sandbox Code Playgroud)

更新:从PEP 484开始,已经被Python 3.5接受,它也是使用注释指定参数和返回类型的官方约定.

  • ...并且有几个包使用这些anntoations来执行运行时类型检查.这比使用断言更方便使用和更容易阅读,并且可以选择性地使用它们.``typecheck-decorator``就是这样一个包,并在其文档中有其他的摘要.(也很灵活:你甚至可以做类型检查鸭打字!) (4认同)

dfr*_*kow 5

PyCharm从@type pydoc字符串中提取类型。在此处此处查看PyCharm文档,以及Epydoc文档。它位于PyCharm的“旧版”部分,也许缺少某些功能。

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.
Run Code Online (Sandbox Code Playgroud)

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

我的目的不是要从CrazyCoder或原始提问者那里窃取分数,而应尽其所能。我只是以为简单的答案应该在“答案”栏中。