如何在 PyCharm 解析器的文档字符串中指定 class 或函数类型
How to specify class or function type in docstring for PyCharm parser
我经常使用 Pycharm 文档字符串类型解析器来指定方法参数和 return、属性或实例变量的类型。如果它几乎一直有效,我有一个小问题告诉 PyCharm 我正在给一个函数或 class 作为 parameter/attribute/...
这是一个简短的例子:
class Bar:
def __init__(self, bar):
"""
:type bar: str
"""
print bar
class Foo:
"""
:type my_class: Bar.__class__
"""
def __init__(self, cinstance=Bar):
"""
:type cinstance: Bar.__class__
"""
self.my_class = cinstance
def run(self):
# it should print an unexpected type warning, but it doesn't.
self.my_class(2)
如果我只用Bar
代替Bar.__class__
,当然PyCharm告诉我Bar
不能调用。那么如何告诉他我正在给他 class ?
请注意,使用 @classmethod
装饰器,PyCharm 可以毫无问题地理解我们在谈论 class 而不是实例。
这是我的尝试:
如果要指定参数的类型为SomeClass
,则应声明为:
@type (param): SomeClass
正确指定,你应该得到这样的东西:
如果您没有正确指定参数类型:
在这一点上,我想我应该更深入地挖掘一下,看看是否能找到任何有趣的东西。如果您转到对象 .__class__
的声明,您将被引导至此处(在 builtins.py
中):
也许 __class__
被设置为 None
?即使它是默认的,但在 class 被实例化时更新(这是我对发生的事情的猜测),这可能是 PyCharm 推断 __class__
解析的结果。所有这些都只是我的猜测,也可能是不正确的,但是......只是为了它,如果我们将参数的类型设置为 None
,那么我们会看到什么行为?
看起来与我们将类型设置为 SomeClass.__fake__
时发生的事情相同(我用 SomeClass.__class__
对其进行了测试,那里也发生了同样的事情。)
所以我想手头的问题是,为什么你不能使用 @type cinstance: Bar
?
PyCharm 支持告诉我以下内容:
As PyCharm developer said: You cannot distinguish classes and instances in type hints. The name of a class in a type hint means that an instance of that class is expected. If your function accepts the class itself, your options are either not to use type hints at all or use the 'type' as a class name. Anyway, there won't be any useful code completion in these cases. See also https://youtrack.jetbrains.com/issue/PY-11615.
指定参数的唯一方法是 class 是使用 :type arg: type
,但完成效果不佳。目前没有其他办法。
对于函数,指定使用 callable
适用于 PyCharm。
试试这个:
from typing import Type
def __init__(self, klass: Type[Bar]):
pass
我经常使用 Pycharm 文档字符串类型解析器来指定方法参数和 return、属性或实例变量的类型。如果它几乎一直有效,我有一个小问题告诉 PyCharm 我正在给一个函数或 class 作为 parameter/attribute/...
这是一个简短的例子:
class Bar:
def __init__(self, bar):
"""
:type bar: str
"""
print bar
class Foo:
"""
:type my_class: Bar.__class__
"""
def __init__(self, cinstance=Bar):
"""
:type cinstance: Bar.__class__
"""
self.my_class = cinstance
def run(self):
# it should print an unexpected type warning, but it doesn't.
self.my_class(2)
如果我只用Bar
代替Bar.__class__
,当然PyCharm告诉我Bar
不能调用。那么如何告诉他我正在给他 class ?
请注意,使用 @classmethod
装饰器,PyCharm 可以毫无问题地理解我们在谈论 class 而不是实例。
这是我的尝试:
如果要指定参数的类型为SomeClass
,则应声明为:
@type (param): SomeClass
正确指定,你应该得到这样的东西:
如果您没有正确指定参数类型:
在这一点上,我想我应该更深入地挖掘一下,看看是否能找到任何有趣的东西。如果您转到对象 .__class__
的声明,您将被引导至此处(在 builtins.py
中):
也许 __class__
被设置为 None
?即使它是默认的,但在 class 被实例化时更新(这是我对发生的事情的猜测),这可能是 PyCharm 推断 __class__
解析的结果。所有这些都只是我的猜测,也可能是不正确的,但是......只是为了它,如果我们将参数的类型设置为 None
,那么我们会看到什么行为?
看起来与我们将类型设置为 SomeClass.__fake__
时发生的事情相同(我用 SomeClass.__class__
对其进行了测试,那里也发生了同样的事情。)
所以我想手头的问题是,为什么你不能使用 @type cinstance: Bar
?
PyCharm 支持告诉我以下内容:
As PyCharm developer said: You cannot distinguish classes and instances in type hints. The name of a class in a type hint means that an instance of that class is expected. If your function accepts the class itself, your options are either not to use type hints at all or use the 'type' as a class name. Anyway, there won't be any useful code completion in these cases. See also https://youtrack.jetbrains.com/issue/PY-11615.
指定参数的唯一方法是 class 是使用 :type arg: type
,但完成效果不佳。目前没有其他办法。
对于函数,指定使用 callable
适用于 PyCharm。
试试这个:
from typing import Type
def __init__(self, klass: Type[Bar]):
pass