我如何告诉 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 表明这一点,这样它就可以向我显示所有 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 farmer: Person
行。
如果您还转到“文件”>“设置> Python Integrated Tools 并将“Docstring format”设置为“Epytext”,然后PyCharm的View >快速文档查找将漂亮地打印参数信息,而不是仅按原样打印所有@行。
When it comes to constructors, and assignments, and method calls, the PyCharm IDE is pretty good at analyzing my source code and figuring out what type each variable should be. I like it when it's right, because it gives me good code-completion and parameter info, and it gives me warnings if I try to access an attribute that doesn't exist.
But when it comes to parameters, it knows nothing. The code-completion dropdowns can't show anything, because they don't know what type the parameter will be. The code analysis can't look for warnings.
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
This makes a certain amount of sense. Other call sites could pass anything for that parameter. But if my method expects a parameter to be of type, say, pygame.Surface
, I'd like to be able to indicate that to PyCharm somehow, so it can show me all of Surface
's attributes in its code-completion dropdown, and highlight warnings if I call the wrong method, and so on.
Is there a way I can give PyCharm a hint, and say "psst, this parameter is supposed to be of type X
"? (Or perhaps, in the spirit of dynamic languages, "this parameter is supposed to quack like an X
"? I'd be fine with that.)
EDIT: CrazyCoder's answer, below, does the trick. For any newcomers like me who want the quick summary, here it is:
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.
The relevant part is the @type peasant: Person
line of the docstring.
If you also go to File > Settings > Python Integrated Tools and set "Docstring format" to "Epytext", then PyCharm's View > Quick Documentation Lookup will pretty-print the parameter information instead of just printing all the @-lines as-is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您可以对方法及其参数使用特殊的文档格式,以便 PyCharm 可以知道类型。最新的 PyCharm 版本 支持最常见的文档格式。
例如,PyCharm 从 @param 样式注释 中提取类型。
另请参阅 reStructuredText 和 文档字符串约定 (PEP 257)。
另一个选择是 Python 3 注释。
请参阅 PyCharm 文档部分了解更多详细信息和样品。
Yes, you can use special documentation format for methods and their parameters so that PyCharm can know the type. Recent PyCharm version supports most common doc formats.
For example, PyCharm extracts types from @param style comments.
See also reStructuredText and docstring conventions (PEP 257).
Another option is Python 3 annotations.
Please refer to the PyCharm documentation section for more details and samples.
如果您使用的是Python 3.0或更高版本,您还可以在函数和参数上使用注释。 PyCharm 会将这些解释为参数或返回值预期具有的类型:
有时这对于不需要文档字符串的非公共方法很有用。作为一个额外的好处,这些注释可以通过代码访问:
更新:自 PEP 484,已被 Python 3.5 接受,它也是使用注释指定参数和返回类型的官方约定。
If you are using Python 3.0 or later, you can also use annotations on functions and parameters. PyCharm will interpret these as the type the arguments or return values are expected to have:
Sometimes this is useful for non-public methods, that do not need a docstring. As an added benefit, those annotations can be accessed by code:
Update: As of PEP 484, which has been accepted for Python 3.5, it is also the official convention to specify argument and return types using annotations.
PyCharm 从 @type pydoc 字符串中提取类型。请参阅 PyCharm 文档 此处 和 此处,以及 Epydoc 文档。它位于 PyCharm 的“遗留”部分,也许缺少一些功能。
相关部分是文档字符串的
@type farmer: Person
行。我的目的不是从 CrazyCoder 或原始提问者那里窃取分数,无论如何都要给他们分数。我只是认为简单的答案应该在“答案”槽中。
PyCharm extracts types from a @type pydoc string. See PyCharm docs here and here, and Epydoc docs. It's in the 'legacy' section of PyCharm, perhaps it lacks some functionality.
The relevant part is the
@type peasant: Person
line of the docstring.My intention is not to steal points from CrazyCoder or the original questioner, by all means give them their points. I just thought the simple answer should be in an 'answer' slot.
我正在使用 PyCharm Professional 2016.1 编写 py2.6-2.7 代码,我发现使用 reStructuredText 我可以以更简洁的方式表达类型:
请参阅: https://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html#legacy
I'm using PyCharm Professional 2016.1 writing py2.6-2.7 code, and I found that using reStructuredText I can express types in a more succint way:
See: https://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html#legacy
您还可以断言类型,Pycharm 会推断它:
You can also assert for a type and Pycharm will infer it: