2011-06-11 72 views
158

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

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

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”,我会罚款吗?)。


编辑: 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. 

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

如果您还要转到文件>设置> Python集成工具并将“文档字符串格式”设置为“Epytext”,则PyCharm的视图>快速文档查找将漂亮地打印参数信息,而不是仅打印所有@ -lines原样。

+7

这是要注意的是reStructuredText的评论只使用有不同的写法相同的标签:'@参数XX:yyy'成为':PARAM XX: yyy'。请参阅http://www.jetbrains.com/pycharm/webhelp/creating-documentation-comments.html – Wernight 2012-04-22 23:13:17

+1

为什么我们不能指定完全限定的类名? – aitchnyu 2014-12-31 09:35:07

回答

76

是的,您可以使用特殊文档格式的方法及其参数,以便PyCharm可以知道类型。最近的PyCharm版本supports most common doc formats

例如,PyCharm从@param style comments中提取类型。

另请参阅reStructuredTextdocstring conventions(PEP 257)。

另一种选择是Python 3注释。

请致电refer to the PyCharm documentation section了解更多详情和样品。

+1

我认为PyCharm稍微改变了它的doc格式(请参阅https://www.jetbrains.com/help/pycharm/using-docstrings-to-specify-types.html),但谢谢!对参数缺乏智能感使我疯狂。 – stubs 2017-10-15 19:56:33

43

如果您使用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,也可指定参数和返回值使用注释类型的正式会议作为一个额外的好处,这些注释可以通过代码访问。

+4

...并且有几个软件包使用这种尝试来执行运行时类型检查。这比使用声明做同样的事情更方便,更易于阅读,并且可以有选择地使用相同的内容。 ''typecheck-decorator''就是这样一个包,并且在其文档中有其他的摘要。 (也是灵活的:你甚至可以进行打字鸭式打字!) – 2014-06-20 15:53:39

3

PyCharm从@type pydoc字符串中提取类型。参见PyCharm文档herehereEpydoc docs。它位于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. 

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

我的目的不是为了从CrazyCoder或原始提问者那里窃取点数,尽量给他们点数。我只是认为简单的答案应该放在'答案'的位置。

0

我使用PyCharm专业2016.1写作py2.6-2.7代码,我发现,使用新结构化,我可以在一个更succint方式表达类型:

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

1

你也可以断言的类型和Pycharm会来推断:

def my_function(an_int): 
    assert isinstance(an_int, int) 
    # Pycharm now knows that an_int is of type int 
    pass 
相关问题