Python 我如何告诉PyCharm参数应该是什么类型?

Python 我如何告诉PyCharm参数应该是什么类型?,python,pycharm,code-completion,type-hinting,Python,Pycharm,Code Completion,Type Hinting,当涉及到构造函数、赋值和方法调用时,PyCharm IDE非常擅长分析我的源代码并找出每个变量应该是什么类型。我喜欢它的正确性,因为它给了我很好的代码完成和参数信息,如果我试图访问一个不存在的属性,它会给我警告 但当涉及到参数时,它一无所知。代码完成下拉列表不能显示任何内容,因为它们不知道参数将是什么类型。代码分析无法查找警告 class Person: def __init__(self, name, age): self.name = name sel

当涉及到构造函数、赋值和方法调用时,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
这是有一定道理的。其他调用站点可以传递该参数的任何内容。但是如果我的方法希望参数的类型是,比如说,
pygame.Surface
,我希望能够以某种方式向PyCharm指出这一点,这样它就可以在代码完成下拉列表中显示
Surface
的所有属性,并在调用错误的方法时突出显示警告,等等

有没有办法给PyCharm一个提示,然后说“psst,这个参数应该是X类型的”?(或者,根据动态语言的精神,“这个参数应该像X一样嘎嘎作响”?我可以接受。)


EDIT: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.
相关部分是docstring的
@type farmer:Person


如果您也转到文件>设置>Python集成工具并将“Docstring format”设置为“Epytext”,那么PyCharm的视图>快速文档查找将非常漂亮地打印参数信息,而不是只按原样打印所有@-行。

是,您可以对方法及其参数使用特殊的文档格式,以便PyCharm可以知道其类型。最新版本

例如,PyCharm从中提取类型

另见和(PEP 257)

另一个选项是Python 3注释


请查看更多详细信息和示例。

如果您使用的是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
有时,这对于不需要docstring的非公共方法很有用。作为一个额外的好处,这些注释可以通过以下代码访问:

>>> King.repress.__annotations__
{'peasant': <class '__main__.Person'>, 'return': <class 'bool'>}
>King.press.\uu注释__
{'farmer':,'return':}

更新:自年起,Python3.5已经接受了这一点,使用注释指定参数和返回类型也是官方惯例。

PyCharm从@type pydoc字符串中提取类型。请参阅PyCharm文档和,以及。它位于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.
相关部分是docstring的
@type farmer:Person


我的意图不是从CrazyCoder或原始提问者那里窃取分数,而是尽一切可能给他们分数。我只是觉得简单的答案应该放在“答案”栏中。

我正在使用PyCharm Professional 2016.1编写py2.6-2.7代码,我发现使用StructuredText可以更简洁地表达类型:

class Replicant(object):
    pass


class Hunter(object):
    def retire(self, replicant):
        """ Retire the rogue or non-functional replicant.
        :param Replicant replicant: the replicant to retire.
        """
        replicant.knock_over()  # Shows a warning.

请参阅:

您还可以为类型断言,Pycharm将推断出:

def my_函数(一个int):
断言isinstance(an_int,int)
#Pycharm现在知道一个int是int类型的
通过

需要注意的是,StructuredText注释使用相同的标记,只是写得不同:
@param xx:yyy
变成了
:param xx:yyy
。看看为什么我们可以不指定完全限定的类名?…有几个包使用这样的注释来执行运行时类型检查。这比通过断言进行同样的操作更方便使用,也更易于阅读,并且可以同样有选择地使用
typecheck decorator
就是这样一个包,它的文档中有其他包的摘要。(也很灵活:您甚至可以进行类型检查鸭子键入!)我认为PyCharm稍微改变了它的文档格式(请参阅),但谢谢!对参数缺乏智能感知让我发疯。