我怎样才能告诉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
这在一定程度上是有意义的。其他呼叫站点可以为该参数传递任何内容。但是,如果我的方法期望参数的类型为
pygame.Surface
,那么我希望能够以某种方式向PyCharm指出,因此它可以在其代码完成下拉列表中向我展示all2ѭ的所有属性,并突出显示警告(如果我调用了错误的方法),依此类推。 有没有办法给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.
相关部分是文档字符串的“ 4”行。 如果您还转到文件>设置> Python集成工具,并将\“ Docstring格式\”设置为\“ Epytext \”,则PyCharm \的视图>快速文档查找将漂亮地打印参数信息,而不仅仅是打印所有@-行原样。     
已邀请:
        是的,您可以对方法及其参数使用特殊的文档格式,以便PyCharm可以知道类型。最新的PyCharm版本支持大多数常见的文档格式。 例如,PyCharm从@param样式注释中提取类型。 另请参见reStructuredText和docstring约定(PEP 257)。 另一个选择是Python 3注释。 请参阅PyCharm文档部分以获取更多详细信息和示例。     
        如果使用的是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
有时,这对于不需要文档字符串的非公共方法很有用。另外一个好处是,这些注释可以通过代码访问:
>>> King.repress.__annotations__
{\'peasant\': <class \'__main__.Person\'>, \'return\': <class \'bool\'>}
更新:从PEP 484(已为Python 3.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.
相关部分是文档字符串的“ 4”行。 我的目的不是要从CrazyCoder或原始提问者那里窃取分数,而应尽其所能。我只是以为简单的答案应该在\'answer \'插槽中。     
        我正在使用PyCharm Professional 2016.1编写py2.6-2.7代码,并且发现使用reStructuredText可以以更简洁的方式表达类型:
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.
参见:https://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html#legacy     
        您还可以断言一个类型,Pycharm会推断出它:
def my_function(an_int):
    assert isinstance(an_int, int)
    # Pycharm now knows that an_int is of type int
    pass
    

要回复问题请先登录注册