2011-06-11 41 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 
相關問題